Windows Socket Network Programming
What is Windows Socket An open interface for network programming under Microsoft Windows. Consists of a collection of function calls, data structure and conventions Provide access to the network services of an underlying protocol stack to any Ms Windows application
Network Program Sketch Open a socket Name the socket Associate with another socket Send and receive between sockets Close sockets
Network vs File I/O File I/O Open a file Read and write Close the file Network I/O Open a socket Name the socket Associate with another socket Send and receive between socket Close the socket
Connection Oriented (TCP) Network Applications Client Server Socket( ) socket( ) Initialize sockaddr_in structure with server (remote) socket name Connect ( )-------------------------------------------- Initialize sockaddr_in structure with server(local) socket name bind( ) listen ( ) accept ( ) <association created, either side can send or receive > Send ( ) ----------------------------------------------- recv ( ) Recv ( ) ---------------------------------------------- send ( ) Closesocket ( ) closesocket ( ) (connected socket) closesocket( ) (listening socket)
Open a Socket To open a socket, we need to call the socket( ) function socket ( int af, int type, int protocol); af = address family indicates the protocol suite in use ( PF_INET) type = socket type indicates the protocol within an address family(sock_dgrm for UDP & SOCK_STREAM for TCP) protocol = protocol to use
Name a Socket A client must be able to locate and identify server sockets, the server application must name its socket to provide connectivity to client To name it socket, the server initializes the socket address structure and call the bind( ) function The socket structure and bind( ) function assign location and identify attributes to socket Struct sockadddr { u_short sa_family; /*address family */ char sa_data[14] /*undefined*/ }; Above is the generic socket address structure.
Name a Socket..cont For the TCP/IP address family(pf_inet), we will always reference the field in the sockaddr_in structure and never those sockaddr structure Struct sockaddr_in { short sin_family; /* address family */ u_short sin_port; /* port(service) number */ structin_addr sin_addr; /* IP address */ char sin_zero[8]; /* unused filler */ Three main attribute should be included in a socket name; protocol, port number, IP address bind( ) function names the local socket with the values in the sockaddr_in structure. int bind ( SOCKET s, struct sockaddr FAR *addr, int namelen); SOCKET s = unbound socket struct sockaddr FAR *addr = local port and IP address int namelen = addr structure length
Associate with another socket The server need to prepare its socket to receive and client need to send When the client is successful, it will create an association between both sockets 5 elements in a socket association; protocol(same for both client and server sockets), client and server IP address, client and server port number 3 steps involve in combining two sockets; server prepare for association, client initiates the association, server completes the association.
How a server prepare for an assocaition For TCP server, it is done by calling listen( ) function. int listen(socket s, int backlog); s = a named unconnected socket backlog = pending connect queue length Backlog parameter is the number of incoming connection request you want to stack to queue while you process the connection your server already accepted. This number >=1 and <=5. The listen( ) function returns zero on success and SOCKET_ERROR if fails. After call listen( ) on a TCP socket, another function call is necessary to prepare to detect the incoming connection request from a client. We can call accept( ) or select( ). Another function call is WSAAsynSelect( ) is the function of choice for any Windows Sockets Application.
How a client initiates an Assocation Since the TCP client is connection-oriented, the function call is connect( ) which initiates the creation of a virtual circuit on a TCP socket or sets a default socket for UDP. Int connect(socket s, struct sockaddr FAR *addr, int namelen); s= socket handle addr = remote port and ip address namelen = addr structure length Parameters for connect( ) is the same as bind( ). Before calling connect, we must initialize the socket address structure.
How a server completes an association TCP server detects an incoming connection attempt when accept( ) is succeed or when select( ) indicates writability on the listening socket. The accept( ) function returns a new socket for a newly created connection after accepting a pending connection request from a listening socket.
Sending Data on a connected socket An application can use send( ) function on TCP socket. int send(socket s, const har FAR *buf, int len, int flag); s= associate socket *buf = buffer with outgoing data len=bytes to sent flag=option flag send( ) can be use only if connect( ) is called successfully. buf parameter is a pointer that point to the first byte to send.
Sending Data on an unconnected socket int sendto(socket s, const char FAR *buf, int len, int flags, struct sockaddr FAR *to, int tolen); s= a valid socket *buf = buffer for outgoing len=bytes to send flags= flags to=remote socket name tolen = length sockaddr For TCP sockets, send( ) and sendto( ) functions are exactly the same.
Receiving Data An application can receive data using recv( ) or recvfrom( ) function. It is a reverse from their counterpart send( ) and sendto( ). The parameters for each pair of function is the same and the have the same socket state.
Close the socket int closesocket(socket s ); s= a valid socket For TCP sockets this function does a quite a bit more compared to UDP because TCP is connection-oriented. Before we close the socket, we can call the shutdown( ) function; it does not release any system resources used by a socket. int shutdown(socket s,int how); s= a valid socket how = flag describing shutdown