Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Formasitchijoh/5beb8487887c515769140dad89d5296f to your computer and use it in GitHub Desktop.
Save Formasitchijoh/5beb8487887c515769140dad89d5296f to your computer and use it in GitHub Desktop.
Multiple client chat system in C
It is an simple implementation of simple client server chat system where once client are connected to server,
then they can communicate with each other.
The client first needs to connect with the server and can then issue two commands -
1. GET - This command fetches the list of client's that are currently connected to server.
2. SEND (client number) (message) - SEND followed by client number which can be be used to send the meassage
to particular to that particular client number.
(please issue the complete send command in one go).
To run these program first run ChatServer.c and then run multiple instatnces of ChatServer.c .
Currently these programs works on same machine.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <pthread.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
void * doRecieving(void * sockID){
int clientSocket = *((int *) sockID);
while(1){
char data[1024];
int read = recv(clientSocket,data,1024,0);
data[read] = '\0';
printf("%s\n",data);
}
}
int main(){
int clientSocket = socket(PF_INET, SOCK_STREAM, 0);
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8080);
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
if(connect(clientSocket, (struct sockaddr*) &serverAddr, sizeof(serverAddr)) == -1) return 0;
printf("Connection established ............\n");
pthread_t thread;
pthread_create(&thread, NULL, doRecieving, (void *) &clientSocket );
while(1){
char input[1024];
scanf("%s",input);
if(strcmp(input,"LIST") == 0){
send(clientSocket,input,1024,0);
}
if(strcmp(input,"SEND") == 0){
send(clientSocket,input,1024,0);
scanf("%s",input);
send(clientSocket,input,1024,0);
scanf("%[^\n]s",input);
send(clientSocket,input,1024,0);
}
}
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <pthread.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
int clientCount = 0;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
struct client{
int index;
int sockID;
struct sockaddr_in clientAddr;
int len;
};
struct client Client[1024];
pthread_t thread[1024];
void * doNetworking(void * ClientDetail){
struct client* clientDetail = (struct client*) ClientDetail;
int index = clientDetail -> index;
int clientSocket = clientDetail -> sockID;
printf("Client %d connected.\n",index + 1);
while(1){
char data[1024];
int read = recv(clientSocket,data,1024,0);
data[read] = '\0';
char output[1024];
if(strcmp(data,"LIST") == 0){
int l = 0;
for(int i = 0 ; i < clientCount ; i ++){
if(i != index)
l += snprintf(output + l,1024,"Client %d is at socket %d.\n",i + 1,Client[i].sockID);
}
send(clientSocket,output,1024,0);
continue;
}
if(strcmp(data,"SEND") == 0){
read = recv(clientSocket,data,1024,0);
data[read] = '\0';
int id = atoi(data) - 1;
read = recv(clientSocket,data,1024,0);
data[read] = '\0';
send(Client[id].sockID,data,1024,0);
}
}
return NULL;
}
int main(){
int serverSocket = socket(PF_INET, SOCK_STREAM, 0);
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8080);
serverAddr.sin_addr.s_addr = htons(INADDR_ANY);
if(bind(serverSocket,(struct sockaddr *) &serverAddr , sizeof(serverAddr)) == -1) return 0;
if(listen(serverSocket,1024) == -1) return 0;
printf("Server started listenting on port 8080 ...........\n");
while(1){
Client[clientCount].sockID = accept(serverSocket, (struct sockaddr*) &Client[clientCount].clientAddr, &Client[clientCount].len);
Client[clientCount].index = clientCount;
pthread_create(&thread[clientCount], NULL, doNetworking, (void *) &Client[clientCount]);
clientCount ++;
}
for(int i = 0 ; i < clientCount ; i ++)
pthread_join(thread[i],NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment