Network Programming with Sockets This section is a brief introduction to the basics of networking programming using the BSD Socket interface on the Unix Operating System. Processes in Unix Sockets Stream Sockets Datagram Sockets System Calls J. Liebeherr 1997 CS 457 - Sockets 1 Process Management in UNIX pid = fork(); Creates a process. The calling process is called parent, created process is called child. The child process is identical to the parent process (One exception: the process number of parent and child are different). fork() returns value 0 to child and child s process number to parent.) J. Liebeherr 1997 CS 457 - Sockets 2
Process Management in UNIX exit(status); Terminates a process and returns status to waiting parent. Processes are created by the fork system call. A fork() A child of A The child is an identical copy of the parent (exception: the process id is different). J. Liebeherr 1997 CS 457 - Sockets 3 Process Management in UNIX Result: Process structure is hierarchical. main() { pid = fork(); if (pid!= 0 ) { /* original process */ printf( original process prints this! ); } else { /* newly created process */ printf( new process prints this! ); } exit(0); } J. Liebeherr 1997 CS 457 - Sockets 4
BSD Sockets Sockets are endpoints of two-way communication paths between two Unix processes. Sockets are one of many mechanisms in a Unix operating system to establish communication between processes. In our programming assignments, we will make extensive use of sockets to enable processes at different machines to talk to another. In fact: Most network applications of the Internet (including: ftp, telnet, mosaic) are written using the socket interface. The abstraction provided by the socket interface is that of a file. A process opens a socket, reads from a socket, writes to a socket, etc. J. Liebeherr 1997 CS 457 - Sockets 5 Sockets Process A socket Process B Kernel Kernel Network J. Liebeherr 1997 CS 457 - Sockets 6
Socket Types and Families Stream Sockets: Bidirectional, reliable, sequenced, and unduplicated data flow. Datagram Sockets: Bidirectional, but unreliable and without guarantee of sequence or duplication. Raw Sockets: Allows direct access to underlying communication protocols. (Requires special privileges.) J. Liebeherr 1997 CS 457 - Sockets 7 Internet Family Sockets Families (Domains) of Sockets: UNIX (AF_UNIX) INTERNET (AF_INET) Other: XNS, IMP etc. J. Liebeherr 1997 CS 457 - Sockets 8
Internet Domain Sockets Sockets and the Internet protocol suite: User Level Socket Layer Stream Sockets TCP Datagram Sockets UDP Internet Protocol (IP) Network Interface J. Liebeherr 1997 CS 457 - Sockets 9 Stream Sockets Stream Sockets establish a communication path that provides a connection-oriented service. The mode of communication between two processes is asymmetric: One process (the Client ) requests a connection (connect). The other process (the Server ) is waiting for a connection (listen) and accepts connection requests (accept). J. Liebeherr 1997 CS 457 - Sockets 10
Stream Sockets Client Algorithm: 1. Find the address (=IP address and port number) of the server with which communication is desired. 2. Allocate a socket. 3. Specify that the connection needs an unused protocol port on the local machine. 4. Connect the socket to the server. 5. Communicate with the server (send and receive). 6. Close the connection. J. Liebeherr 1997 CS 457 - Sockets 11 Stream Sockets Server Algorithm: 1. Create a socket and bind to a well-known address (=IP address and port number) for the service. 2. Place the socket in passive mode, waiting for connections from clients. 3. Accept the next connection request from the socket, and obtain a second socket for the connection. 4. Read and write with client over the second socket. 5. When finished, close the second socket, and continue with Step 3. J. Liebeherr 1997 CS 457 - Sockets 12
Stream Sockets Sequence of system calls: Client socket connect write read close Server socket bind listen accept read write close J. Liebeherr 1997 CS 457 - Sockets 13 Datagram Sockets Connectionless paradigm. Symmetric interface to data exchange. No requirements for connection establishment. Destination address explicitly specified in every message. J. Liebeherr 1997 CS 457 - Sockets 14
Datagram Sockets Client Algorithm: 1. Find the address (=IP address and port number) of the server with which communication is desired. 2. Allocate a socket. 3. Specify that the connection needs an unused protocol port on the local machine. 4. Specify the server to which messages are sent. 5. Communicate with the server (send and receive). 6. Close the socket. J. Liebeherr 1997 CS 457 - Sockets 15 Datagram Sockets Server Algorithm: 1. Create a socket and bind to a well-known address (=IP address and port number) for the service. 2. Repeatedly read the next request from a client, and send back the response. J. Liebeherr 1997 CS 457 - Sockets 16
Datagram Sockets Sequence of system calls: CLIENT socket bind sendto recvfrom close SERVER socket bind recvfrom sendto close J. Liebeherr 1997 CS 457 - Sockets 17 Socket s = socket(family, type, protocol); family: AF_INET for Internet Family. type: SOCK_STREAM, SOCK_DGRAM, SOCK_RAW protocol: 0 (for default), other protocol number. Creates a socket and returns a socket descriptor s. s is an index in the open file table which points to a socket structure. J. Liebeherr 1997 CS 457 - Sockets 18
Bind To make the socket addressable from another process, a socket must have a name bound to it. The bind call is always used by servers, which need to specify a well-known port number. Connectionless clients also need to assure that the system assigns it a unique address. Syntax: bind(s, name, namelen) name: local address which contains an Internet address and a port number; name is of type sockaddr_in. Port numbers: Internet ports below 1024 are reserved for root. If bind is called with port number 0, operating system will assign an unused port number. J. Liebeherr 1997 CS 457 - Sockets 19 Connect Syntax: connect(s, servaddr, addrlen) servaddr: server address which contains an Internet address and a port number; servaddr is of type sockaddr_in. For stream-oriented connections, results in connection establishment between the local system and the foreign system. Assigns association elements: local_addr, local_process, foreign_addr, and foreign_process. After connecting, the process can write to or read from the socket without specifying the foreign address. A process that uses connect does not need to call bind. J. Liebeherr 1997 CS 457 - Sockets 20
Listen Syntax: listen(s, backlog) backlog: specifies how many outstanding connection requests can be queued while waiting for the server to execute accept. Specifies that a connection-oriented server is willing to receive connections. Maximum backlog is 5. J. Liebeherr 1997 CS 457 - Sockets 21 Accept Syntax: accept(s, peer, addrlen) peer: address of the connected peer process (the client). s: specifies socket from which a connection should be accepted. Used after calling socket to create a socket, bind to specify a local endpoint address, and listen to place it in passive mode. Creates a new socket for each new connection request, and returns the descriptor of the new socket to its caller. Original Socket: used to accept new connection. New socket: used only for transferring data on the new connection. J. Liebeherr 1997 CS 457 - Sockets 22
Write / Read Syntax: write(s, buff, nbytes) read(s, buff, nbytes) s: Socket to use for reading/writing. (akin to file descriptor) Similar to reading and writing to files. Used with connection-oriented sockets (which use the connect and accept system calls). Returns the number of bytes read or written. Also the send call: send(s, buff, nbytes, flags) The flags specify options. (e.g. MSG_OOB, MSG_PEEK, etc.) J. Liebeherr 1997 CS 457 - Sockets 23 Sendto / Recvfrom Syntax: sendto(s,buff,nbytes,flags,to,adrlen) recvfrom(s,buff,nbytes,flags,from,adrlen) to (from): Address to (from) which packet is to be sent. adrlen:size of to (or from) argument. For connectionless communication: address is specified for each packet sent by sendto. For a connectionless server, recvfrom fills in protocol-specific address of who sent the data into from. Returns the number of bytes read or written. Also the recv call: recv(s, buff, numbytes, flags) J. Liebeherr 1997 CS 457 - Sockets 24
Types of Servers iterative connectionless iterative connection-oriented concurrent connectionless concurrent connection-oriented J. Liebeherr 1997 CS 457 - Sockets 25