Saturday 2 July 2016

TCP Chat App (Client Server Program in c)

TCP Chat App (Client Server Program in c)

Description: A server client code to implement a chat application. After establishing connection, server should receive and send data in an infinite loop whereas client should send and receive data indefinitely.

Server Code in c

#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;
   welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);
 
   serverAddr.sin_family = AF_INET;
   serverAddr.sin_port = htons(7891);
   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;
   newSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);

   while(1){
      recv(newSocket, buffer, 1024, 0);
      printf("Client says: %s\n",buffer);  
      gets(buffer);
      send(newSocket,buffer,13,0);
   }
  
   return 0;
}



Client Code in c

#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;

   clientSocket = socket(PF_INET, SOCK_STREAM, 0);
   serverAddr.sin_family = AF_INET;
   serverAddr.sin_port = htons(7891);
   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){
     
      gets(buffer);
      send(clientSocket,buffer,13,0);
      recv(clientSocket, buffer, 1024, 0);
      printf("Server says: %s\n",buffer);  
   }

   return 0;
}

No comments

Post a Comment

Recent Posts