UNP Chapter 4: Elementary TCP SocketsIntroductionTypical TCP Client-Server Scenariosocket()socket() parameterssocket() parameterssocket() parametersconnect()connect() parametersbind()bind() parameterslisten()accept()Concurrent Serversclose()getsockname() and getpeername()UNP Chapter 4: Elementary TCP SocketsCMPS 105: Systems ProgrammingProf. Scott BrandtT Th 2-3:45Soc Sci 2, Rm. 167Introduction First: Elementary socket functions Next: TCP client-server example Concurrent services Common Unix server technique Allows server to simultaneously serve multiple clients Server forks a separate process to serve each clientTypical TCP Client-Server Scenario Figure 4.1 (p. 96) Server starts up Client starts up and connects to server Client contacts server Server processes client request Server responds to client Client closes connection Server goes back to waiting for clients to connectsocket() To perform network I/O, a process first calls socket() Called by both client and server #include <sys/socket.h> int socket(intfamily, inttype, intprotocol); returns a socket descriptorsocket() parametersfamilyspecifies the protocol family or domain AF_INET: IPv4 protocols AF_INET6: IPv6 protocols AF_LOCAL: Unix domain protocols AF_ROUTE: Routing sockets AF_KEY: Key socket Usually: AF_INET AF_LOCAL might work on CATS machinessocket() parameterstypespecifies type of communication SOCK_STREAM: stream socket SOCK_DGRAM: datagram socket SOCK_SEQPACKET: sequenced packet socket SOCK_RAW: raw socket TCP supports only SOCK_STREAM UDP supports SOCK_DGRAMsocket() parametersprotocolspecifies transport protocol IPPROTO_TCP: TCP transport protocol IPPROTO_UDP: UDP transport protocol IPPROTO_SCTP: SCTP transport protocol TCP provides reliable, in-order streaming communication Resends lost packets, guarantees order UDP provides possibly unreliable, possibly out-of-order message delivery Doesn’t guarantee anything, but usually works fineconnect() Use by a TCP client to establish a connection with a TCP server #include <sys/socket.h> int connect(intsockfd, const structsockaddr *servaddr, socklen_taddrlen); Returns when the connection has been established Possible errors: timeout, host not found, connection refusedconnect() parameterssockfdis the socket descriptor returned by socket()servaddris the socket address Contains the IP address and port number of the serveraddrlenis the size of the address structurebind() Used by server to bind a socket to an IP address/port pair #include <sys/socket.h> int bind(intsockfd, const structsockaddr *myaddr, socklen_taddrlen); Common error: EADDRINUSEbind() parameterssockfdis the socket descriptor returned by socket()myaddris the address of this server Note: addresses are protocol-specific Usually well-known addresses so that clients can find the serveraddrlenis the length of the addresslisten() Called by a TCP server to: Set the socket up to receive connections Specify the maximum number of connections the kernel should queue up for this socket #include <sys/socket.h> int listen(intsockfd, intbacklog); Normally called after socket() and bind() and before accept()sockfdis the socket descriptor from socket()backlogshould be non-zeroaccept() Called by a TCP server to return the next completed connection from a client Blocks if no connection is available #include <sys/socket.h> int accept(intsockfd, struct sockaddr *cliaddr, socklen_t *addrlen);sockfdis the socket descriptor from socket()cliaddris the address of the client that connected to the server Returns a new socket descriptor for the connection to the clientint main(int argc, char **argv) {int listenfd, connfd;struct sockaddr_in servaddr;char buff[maxline];time_t ticks;listenfd = socket(AF_INET, SOCK_STREAM, 0);bzero(&servaddr, sizeof(servaddr);servaddr.sin_family = AF_INET;servaddr.sin_addr.s_addr = htonl(INADDR_ANY);servaddr.sin_port = htons(13); // daytime severbind(listenfd, (SA *) &servaddr, sizeof(servaddr));listen(listenfd, LISTENQ);while(1) {connfd = accept(listenfd, (SA *)NULL, NULL);ticks = time(NULL);snprintf(buff, sizeof(buff), “%.24s\r\n”, ctime(&ticks));write(connfd, buff, strlen(buff));close(connfd);}}Concurrent Servers The previous example is an iterative server It sequentially receives connections and processes each request in turn A concurrent serverforks a child process to handle each request Multiple requests can be handled in parallel by multiple concurrently executing child processespid_t pid;int listenfd, connfd;listenfd = socket(…);bind(listenfd, …);listen(listenfd, LISTENQ);while(1) {connfd = accept(listenfd, …);if( (pid = fork()) == 0) {close(listenfd);doit(connfd); // service the requestclose(connfd);exit(0);}close(connfd);}close() Normal close() is used to close sockets #include <unistd.h> int close(intsockfd);getsockname() and getpeername() getsockname() returns the local protocol address associate with a socket Used to get socket address when responding to a wildcard connection getpeername() returns the foreign protocol address associated with a socket Used to get client address in an
View Full Document