Single-Client Chat Using TCP Socket

Configure a single-client TCP chat server

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

//This code is written by Souvik Ghosh
//Client 
#include<stdio.h> 
#include<string.h> 
#include<netinet/in.h> 
#include<arpa/inet.h> 
#include<sys/types.h> 
#include<sys/socket.h> 
#include<unistd.h> 
#include<pthread.h> 
#include<termios.h> 
#include<stdlib.h> 

void *recv_thread(void *a); 
void *send_thread(void *a); 
int cfd; 

int main() 
{ 
struct sockaddr_in cl; 
int n; 
pthread_t snd,rcv; 
cfd=socket(AF_INET,SOCK_STREAM,0); 
cl.sin_family=AF_INET; 
inet_aton("127.0.0.1",&(cl.sin_addr)); 
cl.sin_port=htons(8760); 
connect(cfd,(struct sockaddr *)&cl,sizeof(cl)); 
pthread_create(&snd,NULL,send_thread,NULL); 
pthread_create(&rcv,NULL,recv_thread,NULL); 
pthread_join(snd,NULL); 
pthread_join(rcv,NULL); 
close(cfd); 
return 0; 
} 

void *send_thread(void *a) 
{ 
int n; 
char str[200]; 
while(1) 
{ 
fgets(str,200,stdin); 
write(cfd,(void *)str,sizeof(str)); 
if(strncmp(str,"bye",3)==0) 
exit(0); 
} 
pthread_exit(NULL); 
} 
void *recv_thread(void *a) 
{ 
int n; 
char str[200]; 

while(1) 
{ 
n=read(cfd,(void *)str,sizeof(str)); 
if(n>0) 
{ 
printf("Server: %s",str); 
fflush(stdout); 
} 
if(strncmp(str,"bye",3)==0) 
exit(0); 
} 
pthread_exit(NULL);
}

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

//This code is written by Souvik Ghosh
//Server
#include<stdio.h> 
#include<string.h> 
#include<netinet/in.h> 
#include<arpa/inet.h> 
#include<sys/types.h> 
#include<sys/socket.h> 
#include<unistd.h> 
#include<pthread.h> 
#include<termios.h> 
#include<stdlib.h> 
void *recv_thread(void *a); 
void *send_thread(void *a); 
int cfd; 

int main() 
{ 
struct sockaddr_in ser,cl; 
int sfd,len,i,n; 
pthread_t snd,rcv; 
len=sizeof(cl); 
sfd=socket(AF_INET,SOCK_STREAM,0); 
ser.sin_family=AF_INET; 
inet_aton("127.0.0.1",&(ser.sin_addr)); 
ser.sin_port=htons(8760); 
n=bind(sfd,(struct sockaddr*)&ser,sizeof(ser)); 
n=listen(sfd,1); 
cfd=accept(sfd,(struct sockaddr*)&cl,&len); 
pthread_create(&snd,NULL,send_thread,NULL); 
pthread_create(&rcv,NULL,recv_thread,NULL); 
pthread_join(snd,NULL); 
pthread_join(rcv,NULL); 
close(cfd); 
return 0; 
} 

void *send_thread(void *a) 
{ 
int n; 
char str[200]; 
while(1) 
{ 
fgets(str,200,stdin); 
write(cfd,(void *)str,sizeof(str)); 
if(strncmp(str,"bye",3)==0) 
exit(0); 
} 
pthread_exit(NULL); 
} 

void *recv_thread(void *a) 
{ 
int n; 
char str[200]; 
while(1) 
{ 
n=read(cfd,(void *)str,sizeof(str)); 
if(n>0) 
{ 
printf("Client : %s",str); 
fflush(stdout); 
} 
if(strncmp(str,"bye",3)==0) 
exit(0); 
} 
pthread_exit(NULL); 
}

Output

output of single-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 *