28.

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

Sockets: Network IPC Internet Socket UNIX Domain Socket.
Sockets Programming CS144 Review Session 1 April 4, 2008 Ben Nham.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
Windows Sockets Purpose Windows Sockets 2 (Winsock) enables programmers to create advanced internet, intranet, and other network-capable applications to.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
Tutorial 8 Socket Programming
Socket Addresses. Domains Internet domains –familiar with these Unix domains –for processes communicating on the same hosts –not sure of widespread use.
Winsock programming.  TCP/IP UDP TCP  Winsock #include wsock32.lib.
ISP – 9 th Recitation Socket programming – Client side.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Client Software Design Objectives: Understand principles of C/S design, with focus on clients Review Windows implementations of Socket functions.
Network Programming Sockets and Winsock. Please Be Responsible We all know that the Internet is full of security holes –most of them do not require any.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
Socket Programming References: redKlyde ’ s tutorial set Winsock2 for games (gamedev.net)
Operating Systems Chapter 9 Distributed Communication.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
Zhu Reference: Daniel Spangenberger Computer Networks, Fall 2007 PPT-4 Socket Programming.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Computer Systems II CSC 2405 Network Programming.
Sirak Kaewjamnong Computer Network Systems
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
 Wind River Systems, Inc Chapter - 13 Network Programming.
Winsock Programming Blocking and Asynchronous Sockets for Windows.
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS A: Windows Networking A.2. Windows Sockets.
Outline Socket programming with Windows OSSocket programming with Windows OS C++ UDPSocket classC++ UDPSocket class Socket programming with Windows OSSocket.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
CS 158A1 1.4 Implementing Network Software Phenomenal success of the Internet: – Computer # connected doubled every year since 1981, now approaching 200.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
Introduction to Socket
Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Socket Programming Lab 1 1CS Computer Networks.
1 Sockets Programming Socket to me!. 2 Network Application Programming Interface (API) The services provided by the operating system that provide the.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Socket address structures Byte ordering IP address conversion
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
In unistd.h : int gethostname(char * name, int maxlen) Purpose : Find the computer's name.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
1 Socket Interface. 2 Client-Server Architecture The client is the one who speaks first Typical client-server situations  Client and server on the same.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
Client-Server model. Socket programming 
Assignment 3 A Client/Server Application: Chatroom
Socket programming Péter Verhás August 2002
Jim Fawcett CSE 681 – Software Modeling & Analysis Fall 2002
Jim Fawcett CSE 687-OnLine – Object Oriented Design Summer 2017
Jim Fawcett CSE 775 – Distributed Objects Spring 2007
Socket Programming in C
Network Programming with Sockets
Socket Interface 1 Introduction 11 Socket address formats 2 API 12 13
Tutorial on Socket Programming
Sockets.
Transport layer API: Socket Programming
Introduction to Socket Programming
TCP Sockets Programming
30.
Network Programming Chapter 12
Chapter 3 Socket API © Bobby Hoggard, Department of Computer Science, East Carolina University These slides may not be used or duplicated without permission.
Socket Programming(1/2)
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
in unistd.h: int gethostname(char * name, int maxlen)
Sockets.
Jim Fawcett CSE 681 – Software Modeling & Analysis Summer 2003
Presentation transcript:

28

WinSock Server Socket functions int bind( SOCKET s, const struct sockaddr FAR *name, int namelen ); struct sockaddr { u_short sa_family; char sa_data[14]; };

WinSock Server Socket functions A special version of struct sockaddr is used for internet address familty AF_INET, and that defined as struct sockaddr_in struct sockaddr_in { short sin_family; u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; };

WinSock Client Socket functions struct in_addr { union { struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b; struct { u_short s_w1,s_w2; } S_un_w; u_long S_addr; } S_un; }; Host and network byte-ordering: htonl(), htons(), ntohl(), ntohs()

Resolving Host Names struct hostent FAR *gethostbyname( const char FAR *name ); struct hostent { char FAR * h_name; char FAR * FAR * h_aliases; short h_addrtype; short h_length; char FAR * FAR * h_addr_list; };

WinSock Client Socket functions int connect( SOCKET s, const struct sockaddr FAR *name, SERVER’s int namelen ); If socket s, is unbound, unique values are assigned to the local association by the system, and the socket is marked as bound.

Sending and receiving data from server int send( SOCKET s, const char FAR *buf, int len, int flags ); Similarly recv() receives as much data as can fit in to the buffer. Returns # of bytes received. These are blocking calls, i.e. these functions do not return until data is sent/received

Difference between Server and Client socket calls Client: socket() > connect() > send/recv() > closesocket Server: socket() > bind() > listen() > accept() > recv/send () > closesocket

Listen int listen( SOCKET s, int backlog ); Places a socket in a state where it is listening to an incoming connection and can accept it. backlog specifies maximum number of pending connections; It can not be greater than SO_MAXCON Returns 0 if all is well, otherwise SOCKET_ERROR TIP: SOCKET_ERROR is in fact equal to -1

bind associates a local address with a socket. int bind( SOCKET s, const struct sockaddr FAR *name, int namelen ); associates a local address with a socket. If no error, returns 0, SOCKET_ERROR otherwise

Accept() blocking call SOCKET accept( SOCKET s, struct sockaddr FAR *addr, int FAR *addrlen ); Returns a SOCKET descriptor, or, if there is an error returns INVALID_SOCKET The accept function extracts the first connection on the queue of pending connections on socket s. It then creates a new socket and returns a handle to the new socket.

WinSock API calls The blocking problem Synchronous and Asynchronous I/O Non-blocking calls int WSAAsyncSelect( SOCKET s, HWND hWnd, unsigned int wMsg, long lEvent ); Advantage of original BSD socket calls over WinSock function calls

The blocking problem Resolving the blocking problem Multi-threaded solution What is a multi-threaded server? The need for synchronisation objects in multi-threaded server environments

Small WinSock application examples A client showing simple communication to either our own small server, or some server on the internet, e.g. WHOIS servers, HTTP server, time service etc. A small utility that synchronises system time with a source on the internet, accounting for transmission-delays

Introduction to Windows Sockets Programming WHOIS port 43 WHOIS Server: whois.networksolutions.com Description of the functionality of this utility

Advantage of original BSD socket calls over WinSock function calls

Small WinSock application example A WHOIS client with the following functions A dialog-based application Accepts a domain name from user connects to a WHOIS server on the internet, whose name is hard-coded in the application Sends a WHOIS request to the server Shows WHOIS server response in a multi-line edit control

Application User Interface

Application Output

application example #define WHOIS_PORT 43 #define WHOIS_SERVER_NAME "whois.networksolutions.com" Other servers could be "whois.pknic.net.pk" or "whois.crsnic.net" etc. #define MAX_DOMAIN_LEN 25 #define BUFFER_SIZE 16384 BOOL CALLBACK mainDialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam); SOCKET clientSocket;

application example int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WSADATA wsaData; HOSTENT *ptrHostEnt; struct sockaddr_in serverSockAddr; // the address of the socket to connect to int abc; // try initialising the windows sockets library if(WSAStartup( MAKEWORD(1,1), &wsaData)) // request WinSock ver 1.1 MessageBox(NULL, "Error initialising sockets library.", "WinSock Error", MB_OK | MB_ICONSTOP); return 1; }

application example if(!(ptrHostEnt = gethostbyname(WHOIS_SERVER_NAME))) { MessageBox(NULL, "Could not resolve WHOIS server name.", "WinSock Error", MB_OK | MB_ICONSTOP); WSACleanup(); return 1; }

application example // fill out the address of the server. serverSockAddr.sin_family = AF_INET; // fill the address structure with appropriate values serverSockAddr.sin_port = htons(WHOIS_PORT); // MUST convert to network byte-order memset(serverSockAddr.sin_zero, 0, sizeof(serverSockAddr.sin_zero)); memcpy(&serverSockAddr.sin_addr.S_un.S_addr, ptrHostEnt->h_addr_list[0], sizeof(unsigned long)); clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(clientSocket == INVALID_SOCKET) { MessageBox(NULL, "Error creating client socket.", "WinSock Error", MB_OK | MB_ICONSTOP); WSACleanup(); return 1; }

application example if(connect(clientSocket, (struct sockaddr *)&serverSockAddr, sizeof(serverSockAddr))) { abc = WSAGetLastError(); MessageBox(NULL, "Error connecting to WHOIS server.", "WinSock Error", MB_OK | MB_ICONSTOP); WSACleanup(); return 1; } if(DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG_MAIN), NULL, mainDialogProc) == 1) MessageBox(NULL, "Error occurred while sending data to WHOIS server.", "WinSock Error", MB_OK | MB_ICONSTOP); return 0;

application example BOOL CALLBACK mainDialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { int wID, wNotificationCode; char domainName[MAX_DOMAIN_LEN+2+1]; // accomodate CR/LF/NULL char result[BUFFER_SIZE], *startOfBuffer = result; int bytesReceived; switch(message) case WM_INITDIALOG: SendDlgItemMessage(hDlg, IDC_EDIT_DOMAIN, EM_LIMITTEXT, MAX_DOMAIN_LEN, 0); return TRUE; break; case WM_COMMAND: wNotificationCode = HIWORD(wParam); wID = LOWORD(wParam); switch(wID)

application example case IDC_BUTTON_SEND: EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_SEND), FALSE); // disable for 2nd use GetDlgItemText(hDlg, IDC_EDIT_DOMAIN, (LPSTR)domainName, MAX_DOMAIN_LEN+1); strcpy(domainName+strlen(domainName), "\r\n"); if(send(clientSocket, (const char *)domainName, strlen(domainName), 0) == SOCKET_ERROR) EndDialog(hDlg, 1); else { bytesReceived = recv(clientSocket, startOfBuffer, BUFFER_SIZE-1, 0); // -1 for NULL while(bytesReceived > 0) // 0:close SOCKET_ERROR:error startOfBuffer += bytesReceived; // move it forward bytesReceived = recv(clientSocket, startOfBuffer, BUFFER_SIZE-(startOfBuffer-result)-1, 0); // -1 for NULL }

application example if(startOfBuffer != result) // something received *startOfBuffer = NULL; // NULL terminate else strcpy(result, "Null Response"); SetDlgItemText(hDlg, IDC_EDIT_RESULT, result); } break; case IDCANCEL: EndDialog(hDlg, 0); return TRUE; default: return FALSE;