Multi-Client Chat Using TCP Socket

Set up a TCP chat server that supports multiple clients

Client-side implementation of multi-client chat using TCP socket

// This code is written by Souvik Ghosh
// Client program 

#include <stdio.h> 
#include <string.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <pthread.h> 
#define MAX_SIZE 50 
char msg[MAX_SIZE]; 

void *recvmg(void *my_sock) 
{ 
 int sock = *((int *)my_sock); 
 int len; 
 // client thread always ready to receive message 
 while((len = recv(sock,msg,MAX_SIZE,0)) > 0) { 
 msg[len] = '\0'; 
 fputs(msg,stdout); 
 } 
} 

int main(int argc,char *argv[]) 
{ 
 pthread_t recvt; 
 int sock_desc; 
 struct sockaddr_in serv_addr; 
 char client_name[MAX_SIZE]; 
 char recvmsg[MAX_SIZE]; 
 strcpy(client_name, argv[1]); 
 
 if((sock_desc = socket(AF_INET, SOCK_STREAM, 0)) < 0) 
 printf("Failed creating socket\n"); 
 bzero((char *) &serv_addr, sizeof (serv_addr)); 
 serv_addr.sin_family = AF_INET; 
 serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 
 serv_addr.sin_port = htons(3000); 
 if (connect(sock_desc, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) { 
 printf("Failed to connect to server\n"); 
 return -1; 
 } 
 
 //creating a client thread which is always waiting for a message 
 pthread_create(&recvt,NULL,(void *)recvmg,&sock_desc); 
 
 char data[MAX_SIZE]; 
 char sendMSG[MAX_SIZE]; 
 while(1){ 
 fgets(data,MAX_SIZE,stdin); 
 strcpy(sendMSG,client_name); 
 strcat(sendMSG,": "); 
 strcat(sendMSG,data); 
 //send the message/reply 
 write(sock_desc,&sendMSG,sizeof(sendMSG)); 
 } 
 exit(0); 
 close(sock_desc); 
 return 0; 
} 

Server-side implementation of multi-client chat using TCP socket

// This code is written by Souvik Ghosh
// Server program 

#include <sys/socket.h> 
#include <stdio.h> 
#include <string.h> 
#include <arpa/inet.h> 
#include <pthread.h> 

pthread_mutex_t mutex; 
int clients[20]; 
int n=0; 

void sendtoall(char *msg,int curr){ 
 int i; 
 pthread_mutex_lock(&mutex); 
 for(i = 0; i < n; i++) { 
 if(clients[i] != curr) { 
 if(send(clients[i],msg,strlen(msg),0) < 0) { 
 printf("sending failure \n"); 
 continue; 
 } 
 } 
 } 
 pthread_mutex_unlock(&mutex); 
} 

void *recvmg(void *client_sock){ 
 int sock = *((int *)client_sock); 
 char msg[500]; 
 int len; 
 while((len = recv(sock,msg,500,0)) > 0) { 
 msg[len] = '\0'; 
 fputs(msg,stdout); 
 sendtoall(msg,sock); 
 } 
} 

int main(){ 
 struct sockaddr_in ServerIp; 
 pthread_t recvt; 
 int sock=0 , Client_sock=0; 
 ServerIp.sin_family = AF_INET; 
 ServerIp.sin_port = htons(3000); 
 ServerIp.sin_addr.s_addr = inet_addr("127.0.0.1"); 
 sock = socket(AF_INET,SOCK_STREAM,0 ); 
 if( bind( sock, (struct sockaddr *)&ServerIp, sizeof(ServerIp)) == -1 ) 
 printf("cannot bind, error!! \n"); 
 else 
 printf("Server Started...\n"); 
 if( listen( sock ,20 ) == -1 ) 
 printf("listening failed!! \n"); 
 while(1){ 
 if( (Client_sock = accept(sock, (struct sockaddr *)NULL,NULL)) < 0 ){ 
 printf("accept failed!!\n"); 
 }else{ 
 printf("new client joins the chat!!\n"); 
 } 
 pthread_mutex_lock(&mutex); 
 clients[n]= Client_sock; 
 n++; 
 // creating a thread for each client 
 pthread_create(&recvt,NULL,(void *)recvmg,&Client_sock); 
 pthread_mutex_unlock(&mutex); 
 } 
 return 0; 
} 

Output

output of multi-client TCP chat program

Related Posts

Artificial Intelligence Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Artificial Intelligence Suggestions! Elevate Your Learning Experience and Excel with Confidence.

Mixed Signal Design Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Mixed Signal Design Suggestions! Elevate Your Learning Experience and Excel with Confidence.

Fiber Optic Communication Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Fiber Optic Communication Suggestions! Elevate Your Learning Experience and Excel with Confidence.

Cyber Security Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Cyber Security Suggestions! Elevate Your Learning Experience and Excel with Confidence.

MAKAUT 7th Semester Examination Questions – 2023

Mobile Communication and Networks (PE-EC701C) Neural Network and Fuzzy Logic Control (PE-EC702C/PEROB701B) Principles of Management (HS-HU701)

CSS Cheatsheet

Transform your web design game with my CSS cheatsheet! Master the art of styling, dive into essential properties and selectors, and create visually stunning websites effortlessly. Whether…

Leave a Reply

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