Socket Programming 1 Introduction In the classic client server model the client sends out requests to the server and the server does some processing with the request s received and returns a reply or replies to the client The terms request and reply here may take on different meanings depending upon the context and method of operation An example of a simple and ubiquitous client server application would be that of a Web server A client Internet Explorer or Netscape sends out a request fo r a particular web page and the web server which may be geographically distant often in a different continent receives and processes this request and sends out a reply which in this case is the web page that was requested The web page is then displayed on the browser client Request Client Server Reply Figure 1 Client Server paradigm Further servers may be broadly classified into two types based on the way they serve requests from clients Iterative servers can serve only one client at a time If two or more clients send in their requests at the same time one of them has to wait until the other client has received service On the other hand concurrent servers can serve multiple clients at the same time Typically this is done by spawning off a new server process on the receipt of a request the original process goes back to listening to new connections and the newly spawned off process serves the request received We can realize the client server communication described above with a set of network protocols like the TCP IP protocol suite for instance The lower level details like the operation of the protocol itself must be clear to you by now In this tutorial we will look at the issue of developing applications for realizing such communication over a network In order to write such applications we need to understand sockets 2 What are sockets Sockets also called Berkeley Sockets owing to their origin can simply be defined as end points for communication To provide a rather crude visualization we could imagine the client and server hosts in Figure 1 being connected by a pipe through which data flow takes place and each end of the pipe can now be construed as an end point Thus a socket provides us with an abstraction or a logical end point for communication There are different types of sockets Stream sockets of type SOCK STREAM are used for connection oriented TCP connections whereas datagram sockets of type SOCK DGRAM are used for UDP based applications Apart from these two there are other socket types defined like SOCK RAW and SOCK SEQPACKET 1 2 1 Socket layer and the Berkeley Socket API Figure 2 shows the TCP IP protocol stack and shows where the Socket layer may be placed Again please be advised that this is just a representation to indicate the level at which we operate when we write network programs using sockets As shown in the figure sockets make use of the lower level network protocols and provide the application developer with an interface to the lower level network protocols A library of system calls are provided by the socket layer and are termed as the Socket API These system calls can be used in writing socket programs In the sections that ensue we will study those system calls in detail Application Layer Sockets Transport Layer Network Layer Data Link Layer Physical Layer Figure 2 TCP IP protocol stack 2 2 Programming environment and the WinSock library We will study socket programming on the windows environment Windows 9x NT 2000 XP Socket programming on UNIX based environments is similar and most of the system calls used are the same Windows adds its own windows specific extensions Windows provides the WinSock library which contains the system calls used for socket programming on the windows environment Compilation issues on windows machines is relegated to Appendix I 3 Basic Socket system calls Figure 3 shows the sequence of system calls between a client and server for a connectionoriented protocol 3 1 The socket system call The socket system call creates a socket and returns a handle to the socket created The handle is of data type SOCKET In UNIX based environments the socket descriptor is a non negative integer but Windows uses the SOCKET data type for the same 2 socket bind listen accept Blocking until conn From the client socket Conn establishment connect write read Data request write Data reply read Figure 3 Socket system calls for connection oriented case Socket handles may take any value in the range 0 to INVALID SOCKET 1 socket system call syntax 3 SOCKET sd socket int af int type int protocol Here af is the Address family specification type is the socket type and the protocol field is used to specify the protocol to be used with the address family specified The address family can be one of AF INET for Internet protocols like TCP UDP or AF UNIX for Unix internal protocols AF NS for Xerox network protocols or AF IMPLINK for the IMP link layer The type field is the socket type which may be SOCK STREAM for stream sockets TCP connections or SOCK DGRAM for datagram connections Other socket types are defined too SOCK RAW is used for raw sockets and SOCK SEQPACKET is used for a sequenced packet socket The protocol argument is typically set to 0 You may also specify a protocol argument to use a specific protocol for your application socket system call SOCKET sd socket if sd INVALID SOCKET error condition 3 2 The bind system call The bind system call is used to specify the association Local Address Local Port It is used to bind either connection oriented or connectionless sockets The bind function basically associates a name to an unnamed socket name here refers to three components The address family the host address and the port number at which the application will provide its service The syntax and arguments taken by the bind system call is given below bind syntax int result bind SOCKET sd const struct sockaddr name int namelen Here sd is the SOCKET handle returned by the socket system call before and name points to the SOCKADDR structure and namelen is the length of the parameter name in bytes bind system call int res bind if res SOCKET ERROR error condition 4 Upon success bind returns 0 In case of error bind returns SOCKET ERROR So the return value of the bind system call must be checked against SOCKET ERROR to check if the call succeeded 3 3 The listen system call After creation of the socket and binding to a local port the server has to wait on incoming connection requests The
View Full Document