Wednesday, October 14, 2015

Example of Client-Server Program in C (Using Sockets and TCP)

Below you’ll find an example of a very simple client-server program in C. Basically the client connects to the server, the server sends the message “Hello World”, and the client prints the received message.
Keep in mind that I am configuring the settings manually. If you want your code to be IPV4-IPV6 agnostic, IP agnostic and portable to different plataforms you can use the getaddrinfo() function, as explained in this tutorial.
Second, I am not doing error checking on most function calls. You should implement those checks if you are going to use the code for a real project.
Third, if you want more details about the functions or their arguments please check the man page of each one.
Finally, to test the code you just need to run the server on a terminal and then run the client on a different terminal (or run the server as a background process and then run the client on the same terminal).
Server Code
/****************** SERVER CODE ****************/ #include <stdio.h> #include <sys/socket.h> #include <netinet/in.h> #include <string.h> int main(){   int welcomeSocket, newSocket;   char buffer[1024];   struct sockaddr_in serverAddr;   struct sockaddr_storage serverStorage;   socklen_t addr_size;   /*---- Create the socket. The three arguments are: ----*/   /* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */   welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);      /*---- Configure settings of the server address struct ----*/   /* Address family = Internet */   serverAddr.sin_family = AF_INET;   /* Set port number, using htons function to use proper byte order */   serverAddr.sin_port = htons(7891);   /* Set IP address to localhost */   serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");   /* Set all bits of the padding field to 0 */   memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);     /*---- Bind the address struct to the socket ----*/   bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));   /*---- Listen on the socket, with 5 max connection requests queued ----*/   if(listen(welcomeSocket,5)==0)     printf("Listening\n");   else     printf("Error\n");   /*---- Accept call creates a new socket for the incoming connection ----*/   addr_size = sizeof serverStorage;   newSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);   /*---- Send message to the socket of the incoming connection ----*/   strcpy(buffer,"Hello World\n");   send(newSocket,buffer,13,0);   return 0; }
Client Code

/****************** CLIENT CODE ****************/ #include <stdio.h> #include <sys/socket.h> #include <netinet/in.h> #include <string.h> int main(){   int clientSocket;   char buffer[1024];   struct sockaddr_in serverAddr;   socklen_t addr_size;   /*---- Create the socket. The three arguments are: ----*/   /* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */   clientSocket = socket(PF_INET, SOCK_STREAM, 0);      /*---- Configure settings of the server address struct ----*/   /* Address family = Internet */   serverAddr.sin_family = AF_INET;   /* Set port number, using htons function to use proper byte order */   serverAddr.sin_port = htons(7891);   /* Set IP address to localhost */   serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");   /* Set all bits of the padding field to 0 */   memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);     /*---- Connect the socket to the server using the address struct ----*/   addr_size = sizeof serverAddr;   connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);   /*---- Read the message from the server into the buffer ----*/   recv(clientSocket, buffer, 1024, 0);   /*---- Print the received message ----*/   printf("Data received: %s",buffer);     return 0; }