Her under these version of our post we are going to create a socket program in which two client can communicate to each other through a single server . Just it mean that we are creating a socket program for a simple chat like facebook , messenger and other with any coast. lets see the code. don’t forget to run these code on linux operating system. If you are using a window I will recommend you to use window based linux operating system called ubuntu. you can just download the virtual machine and ubuntu separetly and install. It is open source software.
SERVER.c
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
int main(){
int a,b,c,arr[100],acc1,acc2,sock,list,value,len,num1,num2;
char line[100],line1[100];
struct sockaddr_in server,client;
sock=socket(AF_INET,SOCK_STREAM,0);
if(sock==-1){
perror("unable to find the socket\n");
exit(1);
}
server.sin_family=AF_INET;
server.sin_port=htons(6000);
server.sin_addr.s_addr=INADDR_ANY;
bzero(&server.sin_zero,8);
b=bind(sock,(struct sockaddr *)&server,sizeof(server));
if(b==-1){
perror("unable to bind\n");
exit(1);
}
list=listen(sock,5);
if(list==-1){
perror("listening error");
exit(1);
}
while(1){
printf("connection established waiting for the client\n");
//accept1
len=sizeof(struct sockaddr_in);
acc1=accept(sock,(struct sockaddr *)&client,&len);
//accept2
acc2=accept(sock,(struct sockaddr *)&client,&len);
printf("passing information between client.............\n");
do{
//accept from client1 and send to client2
recv(acc1,line,strlen(line)+1,0);
strcpy(line1,line);
send(acc2,line1,strlen(line1)+1,0);
//accept from client2 and send to clientt1
recv(acc2,line,strlen(line)+1,0);
strcpy(line1,line);
send(acc1,line1,strlen(line1)+1,0);
}
while(strcpy(line,"quit"));
close(acc1);
close(acc2);
return 0;
}
}
CLIENT1.c
/*COMPUTER NETWORK OF THE CLIENT PROCESS*/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<string.h>
int main()
{
int i,sock,n;
char line[100],line1[200],sum[200];
struct sockaddr_in client;
sock=socket(AF_INET,SOCK_STREAM,0);
if(sock==-1){
perror("unable to find the socket\n");
exit(1);
}
client.sin_family=AF_INET;
client.sin_addr.s_addr=inet_addr("127.0.0.1"); //Loop back IP address
client.sin_port=htons(6000);
bzero(&client.sin_zero,8);
i=connect(sock,(struct sockaddr *)&client,sizeof(client));
if(i==-1){
perror("unable to find the server\n");
}
else{
printf("connection Established successfully continue your operation\n");
}
/* After connection, the client can send or receive messages.*/
do{
/* sending message to client1(your friend) */
printf(" write message to your friend............\n: ");
scanf("%s",line);
strcpy(line1,line);
send(sock,line1,strlen(line1)+1,0);
/* Receiving message from client1(your friend */
n=recv(sock,line,strlen(line)+1,0);
printf("message from your friend =%s\n",line);
}
while(strcpy(line,"quit"));
close(sock);
return 0;
}
CLIENT2.c
/*COMPUTER NETWORK OF THE CLIENT PROCESS*/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<string.h>
int main()
{
int i,sock,n;
char line[100],line1[200],sum[200];
struct sockaddr_in client;
sock=socket(AF_INET,SOCK_STREAM,0);
if(sock==-1){
perror("unable to find the socket\n");
exit(1);
}
client.sin_family=AF_INET;
client.sin_addr.s_addr=inet_addr("127.0.0.1"); //Loop back IP address
client.sin_port=htons(6000);
bzero(&client.sin_zero,8);
i=connect(sock,(struct sockaddr *)&client,sizeof(client));
if(i==-1){
perror("unable to find the server\n");
}
else{
printf("connection Established successfully continue your operation\n");
}
/* After connection, the client can send or receive messages.*/
do{
/* sending message to client2(your friend) */
printf("write message to your friend..........\n: ");
scanf("%s",line);
strcpy(line1,line);
send(sock,line1,strlen(line1)+1,0);
/* Recieving message from client2(your friend */
recv(sock,line,strlen(line)+1,0);
printf("Message from your friend=%s\n",line);
}
while(strcpy(line,"quit"));
close(sock);
return 0;
}
don’t forget to comment share and subscribe the site to get the latest update