unp chapter 4 elementary tcp sockets
play

UNP Chapter 4: Elementary TCP Sockets CMPS 105: Systems Programming - PowerPoint PPT Presentation

UNP Chapter 4: Elementary TCP Sockets CMPS 105: Systems Programming Prof. Scott Brandt T Th 2-3:45 Soc Sci 2, Rm. 167 Introduction First: Elementary socket functions Next: TCP client-server example Concurrent services Common


  1. UNP Chapter 4: Elementary TCP Sockets CMPS 105: Systems Programming Prof. Scott Brandt T Th 2-3:45 Soc Sci 2, Rm. 167

  2. Introduction � 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 client

  3. Typical 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 connect

  4. socket() � To perform network I/O, a process first calls socket() � Called by both client and server � # include < sys/socket.h> � int socket(int family , int type , int protocol ); � returns a socket descriptor

  5. socket() parameters � family specifies 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 machines

  6. socket() parameters � type specifies 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_DGRAM

  7. socket() parameters � protocol specifies 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 fine

  8. connect() � Use by a TCP client to establish a connection with a TCP server � # include < sys/socket.h> � int connect(int sockfd , const struct sockaddr * servaddr , socklen_t addrlen ); � Returns when the connection has been established � Possible errors: timeout, host not found, connection refused

  9. connect() parameters � sockfd is the socket descriptor returned by socket() � servaddr is the socket address � Contains the IP address and port number of the server � addrlen is the size of the address structure

  10. bind() � Used by server to bind a socket to an IP address/port pair � # include < sys/socket.h> � int bind(int sockfd , const struct sockaddr * myaddr , socklen_t addrlen ); � Common error: EADDRINUSE

  11. bind() parameters � sockfd is the socket descriptor returned by socket() � myaddr is the address of this server � Note: addresses are protocol-specific � Usually well-known addresses so that clients can find the server � addrlen is the length of the address

  12. listen() � 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(int sockfd , int backlog ); � Normally called after socket() and bind() and before accept() � sockfd is the socket descriptor from socket() � backlog should be non-zero

  13. accept() � 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(int sockfd , struct sockaddr * cliaddr , socklen_t * addrlen ); � sockfd is the socket descriptor from socket() � cliaddr is the address of the client that connected to the server � Returns a new socket descriptor for the connection to the client

  14. int 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 sever bind(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); } }

  15. Concurrent Servers � The previous example is an iterative server � It sequentially receives connections and processes each request in turn � A concurrent server forks a child process to handle each request � Multiple requests can be handled in parallel by multiple concurrently executing child processes

  16. pid_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 request close(connfd); exit(0); } close(connfd); }

  17. close() � Normal close() is used to close sockets � # include < unistd.h> � int close(int sockfd );

  18. 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 execed process

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend