This preview shows page 1. Sign up to view the full content.
Unformatted text preview: whether we are a client or a server. 12.5.3 The connect Function
A client establishes a connection with a server by calling the connect function.
#include <sys/socket.h> int connect(int sockfd, struct sockaddr *serv addr, int addrlen );
returns: 0 if OK, -1 on error 632 CHAPTER 12. NETWORK PROGRAMMING The connect function attempts to establish an Internet connection with the server at socket address serv addr, where addrlen is sizeof(sockaddr in). The connect function blocks until either the connection is successfully established, or an error occurs. If successful, the sockfd descriptor is now ready for reading and writing, and the resulting connection is characterized by the socket pair
(x:y, serv_addr.sin_addr:serv_addr.sin_port) where x is the client’s IP address and y is the ephemeral port that uniquely identiﬁes the client process on the client host. Figure 12.24 shows our open clientfd helper function that a client uses to establish a connection with a server running on host hostname and listening for connection requests on th...
View Full Document
This note was uploaded on 09/02/2010 for the course ELECTRICAL 360 taught by Professor Schultz during the Spring '10 term at BYU.
- Spring '10
- The American