Socket Programming Carsten Griwodz Socket programming 1 What are sockets? ❒ ❒ A way in which operating systems make network programming possible ❍ Offer an API for networking to applications ❍ Hide the details of networking from applications Standard: Berkeley Socket API ❍ Introduced in early Unix versions ❍ Used in Linux, Unix, Windows, MacOS, OpenVMS, OS/400, … ❍ Not limited to Internet protocols Socket programming 2 What are sockets? ❒ An API between the application and the network ❍ Applications send data through the socket over the network to a remote application ❍ Applications receive data through the socket from a remote application ❍ Applications control the time and amount of sending and receiving ❍ Operating systems implement the details Socket programming 3 1 What are sockets? Process Socket Control block with buffers, variables Application Controlled by the appliction Process Controlled by the operating system Control block with buffers, variables Socket Internet Socket programming 4 Socket programming 5 What are sockets? ❒ At the application level ❍ Datatype: int ❍ Very similar for • all protocols • file descriptors • stdin, stdout ❒ Share functions with ❍ Reading and writing files ❍ Writing to the screen ❍ Reading from the keyboard What are sockets? ❒ Refer to structures in the operating system ❍ The protocol in use • TCP, UDP, … ❍ Protocol-specific state information ❍ An open, connected TCP socket needs also • Address of the own machine and application • • • • Address of the remote machine and application Buffers Variables Timers Socket programming 6 2 Typical use of sockets ❒ ❒ TCP and UDP communication Transport layer protocols ❍ End-to-end communication ❍ Rely on the routing functions of IP • Host addressing • Path finding • Host identification ❍ IP in turn relies on the link layer • Packet forwarding from host to host Add transport layer functions ❍ Add addressing of applications ❍ Socket programming 7 Why introduce sockets now? After the Link layer lectures, you expect Networking layer lectures ❒ Instead, you get a sockets lecture ❍ The main goal of sockets is to provide the services of the transport layer to applications ❒ Why? ❍ It is possible to emulate link layer and networking layer functions on layer 4 ❍ You are expected to emulate link layer functions in the mandatory assignment 2 ❒ Socket programming 8 Layer emulation PPPoE – PPP over Ethernet ❍ Used by Internet providers who charge by the minute ❒ IP over ATM ❍ ATM is capable of end-to-end communication ❍ But few people have it ❍ Integrate ATM networks into the greater Internet ❒ Socket programming 9 3 Typical use of sockets ❒ ❒ Sockets can provide access to ❍ Network layer ❍ Link layer But this is useless in assignments ❍ Prohibited to normal users ❍ Major differences between operating systems Socket programming 10 Connection-oriented service Goal: data transfer between ❒ end systems ❒ ❍ Connection Start of communication ❍ ❍ ❍ ❍ Handshaking Initial preparation of data transfer Creates a state in the two machines that communicate End systems know their communication partners During communication ❍ ❍ ❒ End system expects messages from connected end system End system know when messages belong to the connection End of communication ❍ Teardown ❍ New handshake required for re-establishing connection Socket programming 11 Transport layer: TCP Connection-oriented service ❒ TCP ❍ ❍ Transmission Control Protocol Connection-oriented service of the Internet TCP offers ❒ Connections ❍ Handshake, end-system state, teardown ❒ Reliable, ordered, streamoriented data transfer ❍ Loss: acknowledgements and retransmissions ❒ Flow control ❍ Send not faster than receiver can receive ❒ Congestion control ❍ Send slower when the network is congested. Socket programming 12 4 Connectionless service Goal: data transfer between ❒ end systems ❒ As before! ❒ During communication ❍ No connection ❍ Start of communication ❍ ❍ ❍ No connection setup No preparation for data transfer Programs must expect messages at all times ❍ ❍ ❒ No state in the machines Senders don’t know whether messages are expected Sender must identify itself in each message End of communication ❍ No teardown ❍ Just stop sending Socket programming 13 Transport layer: UDP Connectionless service ❒ UDP ❍ ❍ User Datagram Protocol Connectionless service of the Internet UDP offers ❒ No connections ❍ ❒ Send immediately Unreliable, unordered, packetoriented data transfer ❍ ❍ Loss: messages are simply lost Messages arrive exactly as send ❒ No flow control ❒ No congestion control ❍ ❍ Send as fast as programs want to Ignore network problems Socket programming 14 Socket programming 15 Using TCP sockets 5 Creating and using a TCP socket Handshaking Initial preparation of data transfer ❒ Create a state in the two machines that communicate ❒ ❒ Socket programming 16 socket() ❒ ❒ ❒ The application claims a socket Call to the function socket() creates a transport control block, and returns a reference to it The kind of socket is determined at this time ❍ TCP, UDP, … int sock; sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); Socket programming 17 socket() sock=socket(domain,type,protocol) ❒ ❒ PF_INET, SOCK_STREAM and IPPROTO_TCP are constants that are defined in the included files Many other possibilities exist ❍ ❍ ❍ ❒ ❒ domain: PF_UNIX, PF_INET, PF_INET6, … type: SOCK_STREAM, SOCK_DGRAM,… protocol: IPPROTO_TCP, IPPROTO_UDP… PF_INET and SOCK_STREAM request a TCP socket PF_INET and SOCK_DGRAM request a UDP socket Socket programming 18 6 socket() ❒ A TCP state machine is created in closed state CLOSED LISTEN SYN RCVD SYN SENT ESTABLISHED FIN WAIT 1 CLOSING CLOSE WAIT FIN WAIT 2 TIME WAIT LAST ACK Socket programming 19 Creation of a TCP connection ❒ One application must be the active one ❍ ❍ ❒ The other application must be passive ❍ ❍ ❍ ❒ Take the initiative in creating the connection This side is called the client Be prepared for accepting connections Expect someone else to takes the initiative This side is called the server This use of the words client and server is not entirely consistent with everyday use ❍ For programming this is conventional Socket programming 20 Socket programming 21 Addressing in the Internet ❒ Addressing hosts Solved by the network layer IP addresses ❍ IPv4: 32 bit ❍ IPv6: 128 bit ❍ ❍ ❒ Addressing applications Solved by the transport layer Ports ❍ Size: 16 bit ❍ ❍ ❒ Details in lectures on layers 3 and 4 7 Addressing in applications ❒ Addressing hosts ❒ Addressing applications ❍ ❍ Translate the name of the host to an IP address Defined by the application programmer Socket programming 22 TCP server steps ❒ ❒ ❒ Goal: Move the TCP state machine to a waiting state Host address is known Define an application address ❍ The port /* declarations */ struct sockaddr_in serveraddr; /* Clear the structure */ bzero(&serveraddr, sizeof(serveraddr)); /* Set the address family */ serveraddr.sin_family = AF_INET; /* Allow all own addresses to receive */ serveraddr.sin_addr.s_addr = INADDR_ANY; /* Add the port number */ serveraddr.sin_port = htons(2009); Socket programming 23 struct sockaddr_in ❒ Define IP addresses and port numbers in the way the Berkeley socket API needs it ❒ htons() ❍ ❍ Host-to-network short Translate an integer value to network format ❒ AF_INET ❍ Constant indicating that Internet protocols will be used ❒ INADDR_ANY ❍ ❍ Constant meaning any (Internet) address In this context: any own Internet address Socket programming 24 8 Binding and listing ❒ ❒ ❒ ❒ ❒ Goal: Move the TCP state machine to a waiting state Host address is known Define an application address ❍ The port Add address information to socket Go into waiting state /* bind the address to the socket */ bind(request_sock, (struct sockaddr *)&serveraddr, sizeof serveraddr); /* active listening on the socket */ listen(request_sock, SOMAXCONN); /* wait for connections */ clientaddrlen = sizeof(struct sockaddr_in); sock = accept(request_sock, (struct sockaddr *)&clientaddr, &clientaddrlen); ACCEPT FUNCTION BLOCKS Socket programming 25 Server: bind(), listen(), accept() ❒ Moving a server into listen state listen CLOSED bind LISTEN accept SYN RCVD SYN SENT ESTABLISHED FIN WAIT 1 CLOSING CLOSE WAIT FIN WAIT 2 TIME WAIT LAST ACK Socket programming 26 Server: bind(), listen() ❒ bind() ❍ ❍ Tells the socket on the server side which address and port number to listen to A machine can have several addresses ❒ listen(); ❍ ❍ Prepares the server for listening to connect requests, and initializes a queue for connect requests The second parameter defines how long the queue should be ❒ SOMAXCONN ❍ ❍ The length of the queue of connection requests Defined in the includes Socket programming 27 9 Server: accept() ❒ ❒ ❒ ❒ ❒ Returns a new socket that the server can use to communicate with the client Take the first connect request from the connect request queue Wait for the next connect request to arrive if the queue is empty clientaddr contains information about the client clientaddrlen must be initialized, so accept knows the size of clientaddr Socket programming 28 TCP client steps Goal: Move the server’s and client’s state machines to established state ❒ Server’s host name is known ❒ Server’s host address is unknown ❒ Use the same application address ❍ The port ❒ Socket programming 29 Preparing the client for connecting ❒ sockaddr_in serveraddr ❍ ❒ Needed to specify the server’s address for the socket gethostbyname() ❍ ❍ ❍ Takes a hostname Returns information about that hostname Including its IP address char* machine = “login.ifi.uio.no”; struct hostent *hostp; struct sockaddr_in serveraddr; int sock; /* Look in DNS for the IP address of the name */ if ((hostp = gethostbyname(machine)) == 0) { fprintf(stderr,“Unknown machine %s\n",machine); exit(1); } bzero((void *) &serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; memcpy(&serveraddr.sin_addr, hostp->h_addr, hostp->h_length); serveraddr.sin_port = htons(2009); Socket programming 30 10 Preparing the client for connecting ❒ connect() ❍ ❒ sock ❍ ❍ ❒ Take all the steps to initiate a connection to the server The own socket Operating systems fills in own control block implicitly /* Connect */ connect(sock, (struct sockaddr*)&serveraddr, sizeof(struct sockaddr_in)); serveraddr ❍ The address of the TCP server application Socket programming 31 Client: connect() ❒ Advancing the client into established state CLOSED Connect() LISTEN Receive SYN+ACK from server SYN RCVD ESTABLISHED Send SYN to server SYN SENT Send ACK to server FIN WAIT 1 CLOSING CLOSE WAIT FIN WAIT 2 TIME WAIT LAST ACK Socket programming 32 Server: accept() ❒ Advancing the server into established state Send SYN+ACK to client CLOSED LISTEN SYN RCVD Receive ACK from client Receive SYN from client SYN SENT ESTABLISHED FIN WAIT 1 CLOSING CLOSE WAIT FIN WAIT 2 TIME WAIT LAST ACK Socket programming 33 11 Client: connect() ❒ ❒ ❒ Connects client socket to a server that is specified in the address structure Does NOT return a new socket The given socket is later used for communicating contains information about the server ❒ The length of serveraddr is not modified ❒ serveraddr Socket programming 34 Server: accept() ❒ ❒ ❒ Server has left waiting state accept() function returns Processing continues /* bind the address to the socket */ bind(request_sock, (struct sockaddr *)&serveraddr, sizeof serveraddr); /* active listening on the socket */ listen(request_sock, SOMAXCONN); /* wait for connections */ clientaddrlen = sizeof(struct sockaddr_in); sock = accept(request_sock, (struct sockaddr *)&clientaddr, &clientaddrlen); SERVER CAN CONTINUE Socket programming 35 read() and write() ❒ The call read(sock, buffer, n); ❍ Reads n characters ❍ From socket sock ❍ Stores them in the character array buffer ❒ The call write(sock, buffer, n); ❍ Writes n characters ❍ From character array buffer ❍ To the socket sock Socket programming 36 12 read() and write() ❒ State stays the same CLOSED LISTEN SYN RCVD SYN SENT ESTABLISHED read write FIN WAIT 1 CLOSING CLOSE WAIT FIN WAIT 2 TIME WAIT LAST ACK Socket programming 37 read() and write() TCP sender char buffer[2000]; int retval; buffer[0] = ‘a’; buffer[1] = ‘b’; buffer[2] = ‘c’; … ❒ Typical ❍ retval == -1 ❍ retval == 0 ❍ retval n < 2000 • Some kind of error, look at errno • The connection has been closed • You tried to send too fast • Only n bytes have been sent • Try sending the reset /* write abcdefghij */ retval = write( sock, buffer, 2000 ); ❍ retval == 2000 • All bytes have been sent ❒ Very untypical ❍ ❍ ❍ retval < -1 retval > 2000 Both cases: • Have you used char retval instead of int retval? Socket programming 38 read() and write() TCP receiver char buffer[2000]; int retval; … ❒ Typical ❍ retval == -1 ❍ retval == 0 ❍ retval n < 2000 • Some kind of error, look at errno /* write abcdefghij */ retval = read( sock, buffer, 2000 ); • The connection has been closed • Only n bytes have been received • No new data has arrived recently • Try sending the rest ❍ retval == 2000 • All bytes have been received ❒ Very untypical ❍ ❍ ❍ retval < -1 retval > 2000 Both cases: • Have you used char retval instead of int retval? Socket programming 39 13 Closing of sockets Server Client /* Close the socket */ close(sock); ❒ Note that the semantics of close depends ❍ ❍ ❒ /* Close both sockets */ close(sock); close(request_sock); on the kind of protocol some possible extra settings All data that has not been read yet may be thrown away Socket programming 40 close(), initiator ❒ Return to close state CLOSED LISTEN SYN RCVD One side calls close Send FIN to other side close() ESTABLISHED FIN WAIT 1 Recv ACK CLOSING Recv FIN Send ACK FIN WAIT 2 TIME WAIT SYN SENT Timeout CLOSE WAIT LAST ACK Socket programming close(), other ❒ 41 side Return to close state CLOSED SYN RCVD Recv ACK FIN WAIT 1 LISTEN ESTABLISHED CLOSING SYN SENT Recv FIN Send ACK Notify application CLOSE WAIT close() Send FIN FIN WAIT 2 TIME WAIT LAST ACK Socket programming 42 14 Concurrency ❒ Must solve several tasks at once ❍ Wait for data from communication partners ❍ ❍ ❍ Read data typed by the user Listen for client connections (servers only) Socket programming 43 Concurrency ❒ Use fork to create several processes ❒ Use select ❒ Use threads ❍ Each process waits for one event ❍ One process waits for several events ❍ Similar to fork Socket programming 44 fork() ❒ int fork(); ❒ A standard approach for servers ❍ Perform a blocking wait such as ❍ When it returns ❍ The parent process • new_sock = accept(listen_sock); • Call fork() • Closes new_sock • Wait for more connections on listen_sock ❍ The child process • Closes listen_sock • Communicates with the client using new_sock Socket programming 45 15 select() ❒ ❒ ❒ int select( int max_fd, fd_set* read_set, fd_set* write_set, fd_set* except_set, struct timeval* timeout ); ❒ ❒ Complicated at first But very useful Can wait for activity on many sockets ❍ New connections on request sockets ❍ New data on connected sockets ❍ Closing of a connected socket ❍ Ready-to-send on a socket Can wait for user input Can wait for timeouts Socket programming 46 select() ❒ For servers ❍ int select( int max_fd, fd_set* read_set, fd_set* write_set, fd_set* except_set, struct timeval* timeout ); ❍ ❒ Serve many clients at once Handle clients that close connections, clients that crash, … For simpler cases ❍ ❍ Wait for data from chat partner Wait for typing of the user Socket programming 47 select() ❒ read_set ❍ int select( int max_fd, fd_set* read_set, fd_set* write_set, fd_set* except_set, struct timeval* timeout ); ❍ ❍ ❒ write_set ❍ ❍ ❒ Arriving connect requests Arriving data Closing sockets Not often used Non-blocking send is finished except_set ❍ Hardly ever used ❍ sendto(.,.,.,MSG_OOB) Socket programming 48 16 select() void wait_for_all(int clientsock[], int clients) { fd_set read_set; int i,act,top=0; ❒ ❒ Using only the read_set is typical Clear the read_set ❍ FD_ZERO(&read_set); for( i=0; i<clients; i++ ) { FD_SET(clientsock[i],&read_set); top = MAX(top,clientsock[i]); } act = select( top+1, &read_set, NULL, NULL, NULL); … ❒ ❒ ❒ NULL timeout ❍ ❒ Must be done every time Put all sockets into the read set Find the highest socket number, add 1 Means forever Call select ❍ } waits for arriving data Socket programming 49 select() void wait_some_time(int sec, int usec) { struct timeval timeout; timeout.tv_sec = sec; timeout.tv_usec = usec; act = select( 0, NULL, NULL, NULL, &timeout ); … } can also wait only for a timeout ❒ select ❍ ❒ Without sockets Timeout parameter ❍ ❍ ❍ NULL means wait forever Timeval {5,0} means wait 5 seconds Timeval {0,0} means don’t wait at all Socket programming 50 Compilation of socket programs ❒ IFI’s Solaris machines ❍ ❍ gcc client.c –o client –lsocket -lnsl cc client.c –o client -lsocket –lnsl ❒ IFI’s Linux machines ❍ gcc client.c –o client ❒ Cygwin on Windows ❍ gcc client.c –o client Socket programming 51 17 The socket API’s error variable errno ❒ extern int errno ❍ (Can be used like) a variable that exists once ❍ Contains the last socket error ❒ void perror(const char* str) ❍ Prints your string str first, and an explanation for errno afterwards ❒ In /usr/include/errno.h ❍ ❍ ❍ ❍ Defines EINTR • Something strange happened, e.g. an interrupt. Nothing happened. Try again! ENOTSOCK • Your sock variable is not a socket at all EBADFD • The connection has been closed and you haven’t noticed it Socket programming 52 Socket options ❒ Some ways to set socket options ❍ setsockopt( int sock, int level, int optname, void* optvalue, int optlen ) • Options for reading and writing all types of sockets • Often used in setsockopt( request_sock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)); ❍ fcntl( int sock, int optname, long optvalue ); • Options for reading and writing all types of sockets, files, pipes, user input, graphical output • Often used in fcntl( sock, F_SETFL, O_NONBLOCK); to make a socket non-blocking Socket programming 53 Socket options ❒ Some ways to set socket options ❍ ioctl( int sock, int optname, void* arg); • Options for all types of file descriptors • Useful in int num_bytes; ioctl( sock, FIONREAD, &num_bytes); number of bytes that have arrived to check the Socket programming 54 18 Summary ❒ Introduced sockets ❒ Provided details for the use of TCP ❍ According to the Berkeley Socket API Setting up a connection Reading and writing over a connection ❍ Tearing down a connection ❍ Some ways to approach the concurrency problem ❍ Frequently helpful socket options ❍ ❍ Socket programming 55 19