Sockets Programming Example in C: Server Converts Strings to Uppercase


Improve your writing skills in 5 minutes a day with the Daily Writing Tips email newsletter.

Below you’ll find an example of a client-server application that uses sockets programming in C. Basically the server listens for connection requests, and whatever message the client sends the server converts it to uppercase and sends it back. You could see this as the server providing a service of converting strings to uppercase.

The server forks a child process to handle each new client connection, so that the main loop can keep listening for new connection requests.

The client keeps asking the user for a string and converts it using the connection to the server. Once you close the client process the server child process handling it will terminate as well.

If you want more details about how the sockets configuration work check this post where this same code is commented out.

Server Code

/******************* SERVER CODE *****************/

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>

int main(){
  int welcomeSocket, newSocket, portNum, clientLen, nBytes;
  char buffer[1024];
  struct sockaddr_in serverAddr;
  struct sockaddr_storage serverStorage;
  socklen_t addr_size;
  int i;

  welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);

  portNum = 7891;
  
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_port = htons(portNum);
  serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  

  bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));

  if(listen(welcomeSocket,5)==0)
    printf("Listening\n");
  else
    printf("Error\n");

  addr_size = sizeof serverStorage;

  /*loop to keep accepting new connections*/
  while(1){
    newSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);
    /*fork a child process to handle the new connection*/
    if(!fork()){
      nBytes = 1;
      /*loop while connection is live*/
      while(nBytes!=0){
        nBytes = recv(newSocket,buffer,1024,0);
  
        for (i=0;i<nBytes-1;i++){
          buffer[i] = toupper(buffer[i]);
        }

        send(newSocket,buffer,nBytes,0);
      }
      close(newSocket);
      exit(0);
    }
    /*if parent, close the socket and go back to listening new requests*/
    else{
      close(newSocket);
    }
  }

  return 0;
}

Client Code

/******************* CLIENT CODE *****************/

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

int main(){
  int clientSocket, portNum, nBytes;
  char buffer[1024];
  struct sockaddr_in serverAddr;
  socklen_t addr_size;

  clientSocket = socket(PF_INET, SOCK_STREAM, 0);

  portNum = 7891;

  serverAddr.sin_family = AF_INET;
  serverAddr.sin_port = htons(portNum);
  serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  

  addr_size = sizeof serverAddr;
  connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);

  while(1){
    printf("Type a sentence to send to server:\n");
    fgets(buffer,1024,stdin);
    printf("You typed: %s",buffer);

    nBytes = strlen(buffer) + 1;

    send(clientSocket,buffer,nBytes,0);

    recv(clientSocket, buffer, 1024, 0);

    printf("Received from server: %s\n\n",buffer);   
  }

  return 0;
}

2 thoughts on “Sockets Programming Example in C: Server Converts Strings to Uppercase

  1. Tanya

    What if I want the server to send back a different message to the client. I’m not able to take any input on the server side.

    Reply
  2. Johannes Peter

    In the server code, the ‘clientLen’ variable is not used anywhere. What was it originally intended for?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *