Example: Generic client
This 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:
- An iterative server. See Example: Writing an iterative server program.
- A spawn server and worker. See Example: Using the spawn() API to create child processes.
- A sendmsg() server and rcvmsg() worker. See Example: Server program used for sendmsg() and recvmsg().
- A multiple accept() design. See Example: Server program to create a pool of multiple accept() worker jobs.
- A nonblocking I/O and select() design. See Example: Nonblocking I/O and select().
- A server that accepts connections from either an IPv4 or IPv6 client. See Example: Accepting connections from both IPv6 and IPv4 clients.
Socket flow of events: Generic client
The following example program uses the following sequence of API calls:
- The socket() API returns a socket descriptor, which represents an endpoint. The statement also identifies that the INET (Internet Protocol) address family with the TCP transport (SOCK_STREAM) is used for this socket.
- After the socket descriptor is received, the connect() API is used to establish a connection to the server.
- The send() API sends the data buffer to the worker jobs.
- The recv() API receives the data buffer from the worker jobs.
- The close() API closes any open socket descriptors.
Note: By using the 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_in6 addr;
/*************************************************/
/* Create an AF_INET6 stream socket */
/*************************************************/
sockfd = socket(AF_INET6, SOCK_STREAM, 0);
if (sockfd < 0)
{
perror("socket");
exit(-1);
}
/*************************************************/
/* Initialize the socket address structure */
/*************************************************/
memset(&addr, 0, sizeof(addr));
addr.sin6_family = AF_INET6;
memcpy(&addr.sin6_addr, &in6addr_any, sizeof(in6addr_any));
addr.sin6_port = htons(SERVER_PORT);
/*************************************************/
/* Connect to the server */
/*************************************************/
rc = connect(sockfd,
(struct sockaddr *)&addr,
sizeof(struct sockaddr_in6));
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);
}