Example: Generic client

This code example contains the code for a common client job.

The client job does a socket(), connect(), send(), recv(), and close() operation. The client job is not aware that the data buffer it sent and received is going to a worker job rather than the server. If you want to create a client application that works whether the server uses the AF_INET address family or AF_INET6 address family, use the IPv4 or IPv6 client example.

This client job works with each of these common connection-oriented server designs:

Socket flow of events: Generic client

The following sample program uses the following sequence of function calls:

  1. The socket() function returns a socket descriptor representing an endpoint. The statement also identifies that the INET (Internet Protocol) address family with the TCP transport (SOCK_STREAM) being used for this socket.
  2. After the socket descriptor is received, the connect() function is used to establish a connection to the server.
  3. The send() function sends data buffer to the worker job(s).
  4. The recv() function receives data buffer from the worker job(s).
  5. The close() function closes any open socket descriptors.
Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.
/**************************************************************************/
/* Generic client example is used with connection-oriented server designs */
/**************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define SERVER_PORT  12345

main (int argc, char *argv[])
{
   int    len, rc;
   int    sockfd;
   char   send_buf[80];
   char   recv_buf[80];
   struct sockaddr_in   addr;

   /*************************************************/
   /* Create an AF_INET stream socket               */
   /*************************************************/
   sockfd = socket(AF_INET, SOCK_STREAM, 0);
   if (sockfd < 0)
   {
      perror("socket");
      exit(-1);
   }

   /*************************************************/
   /* Initialize the socket address structure       */
   /*************************************************/
   memset(&addr, 0, sizeof(addr));
   addr.sin_family      = AF_INET;
   addr.sin_addr.s_addr = htonl(INADDR_ANY);
   addr.sin_port        = htons(SERVER_PORT);

   /*************************************************/
   /* Connect to the server                         */
   /*************************************************/
   rc = connect(sockfd,
                (struct sockaddr *)&addr,
                sizeof(struct sockaddr_in));
   if (rc < 0)
   {
      perror("connect");
      close(sockfd);
      exit(-1);
   }
   printf("Connect completed.\n");

   /*************************************************/
   /* Enter data buffer that is to be sent          */
   /*************************************************/
   printf("Enter message to be sent:\n");
   gets(send_buf);

   /*************************************************/
   /* Send data buffer to the worker job            */
   /*************************************************/
   len = send(sockfd, send_buf, strlen(send_buf) + 1, 0);
   if (len != strlen(send_buf) + 1)
   {
      perror("send");
      close(sockfd);
      exit(-1);
   }
   printf("%d bytes sent\n", len);

   /*************************************************/
   /* Receive data buffer from the worker job       */
   /*************************************************/
   len = recv(sockfd, recv_buf, sizeof(recv_buf), 0);
   if (len != strlen(send_buf) + 1)
   {
      perror("recv");
      close(sockfd);
      exit(-1);
   }
   printf("%d bytes received\n", len);

   /*************************************************/
   /* Close down the socket                         */
   /*************************************************/
   close(sockfd);
}
Related reference
Example: IPv4 or IPv6 client
Examples: Connection-oriented designs
Example: Write an iterative server program
Example: Pass descriptors between processes
Example: Server program used for sendmsg() and recvmsg()
Examples: Use multiple accept() APIs to handle incoming requests
Example: Server program to create a pool of multiple accept() worker jobs
Example: Use asynchronous I/O
Example: Nonblocking I/O and select()
Related information
socket()
connect()
close()
send()
recv()