Http10 is a previous version from 1996 that is still

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: r from bind. */ optval = 1; Setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval , sizeof(int)); /* listenfd will be an endpoint for all requests to port on any IP address for this host */ bzero((char *) &serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); serveraddr.sin_port = htons((unsigned short)port); Bind(listenfd, (SA *)&serveraddr, sizeof(serveraddr)); /* make it a listening socket ready to accept connection requests */ Listen(listenfd, LISTENQ); return listenfd; } code/src/csapp.c Figure 12.25: open listenfd: helper function that opens and returns a listening socket. 12.5. THE SOCKETS INTERFACE 635 12.5.6 The accept Function Servers wait for connection requests from clients by calling the accept function. #include <sys/socket.h> int accept(int listenfd, struct sockaddr *addr, int *addrlen); returns: nonnegative connected descriptor if OK, -1 on error The accept function waits for a connection request from a client to arrive on the listening descriptor listenfd, then fills in the client’s socket address in addr, and returns...
View Full Document

Ask a homework question - tutors are online