On the client, the connect() system call establishes a connection to the server.

int connect(int sockfd, struct sockaddr *serv_addr, int addrlen);

The first parameter is the socket descriptor from the socket() call, sockfd.

The second parameter is the server’s sockaddr address, to be filled in.

The third parameter is the length of the server’s sockaddr structure.

This is similar to the accept() system call, except that the client does not have to bind a local address to the socket descriptor before calling connect(). The server address pointed to by srv_addr must exist.

For example:

#define SERV_PORT 5432
     unsigned long inet_addr(char *ptr);
     bzero((char *) &serv_addr, sizeof(serv_addr));
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_port = htons(SERV_PORT):
     serv_addr.sin_addr.s_addr = inet_addr(SERV_HOST_ADDR);
     connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));