MULTI USER CHAT USING SOCKETS

Server program

 #include<stdio.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
 int sd,sd1,i,clilen,len,sport;
 struct sockaddr_in ser,cli;
 char smsg[20],rmsg[20];
printf("\n\t Enter the Port : ");
scanf("%d",&sport);
if((sd=socket(AF_INET,SOCK_STREAM,0))<0)
{
  printf("\n\t Error in socket");
  return 0;
}
bzero(&ser,sizeof(ser));
ser.sin_family=AF_INET;
ser.sin_port=htons(sport);
ser.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(sd,(struct sockaddr*)&ser,sizeof(ser)) <0)
{
  printf("\n\t Error in bind");
  return 0;
}
if(listen(sd,5) <0)
{
 printf("\n\t Error in Listen");
 return 0;
}
do
{
printf("\n\t Enter the client no to be communicate : ");
scanf("%d",&i);
 if(i==0)
   exit(0);
printf("\n\t Client %d is connected",i);
clilen=sizeof(cli);
if((sd1=accept(sd,(struct sockaddr*)&cli,&clilen)) <0)
{
 printf("\n\t Error in Accept");
 return 0;   }

 printf("\n\t Accepted ");
  do
 {
  recv(sd1,rmsg,20,0);
  printf("\n\t Clinet Msg Received : %s ",rmsg);
 printf("\n\t Enter Server String : ");
 scanf("%s",smsg);
 send(sd1,smsg,20,0);
 wait(20);
 } while((strcmp(smsg,"exit"))!=0);
} while(i!=0);
  close(sd);
}

Client :1  program

 #include<stdio.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
int sd,len,cport;
struct sockaddr_in ser;
char smsg[20],rmsg[20];
printf("\n\t Enter the Port : ");
scanf("%d",&cport);
if((sd=socket(AF_INET,SOCK_STREAM,0))<0)
{
  printf("\n\t Error in socket");
  return 0;
}

bzero(&ser,sizeof(ser));
ser.sin_family=AF_INET;
ser.sin_port=htons(cport);
ser.sin_addr.s_addr=htonl(INADDR_ANY);

if(connect(sd,(struct sockaddr*)&ser,sizeof(ser)) <0)
{
  printf("\n\t Error in connect");
  exit(-1);
}

do
{

 printf("\n\t Enter the Client String :");
 scanf("%s",smsg);
 send(sd,smsg,20,0);
 wait(20);
 recv(sd,rmsg,20,0);
 printf("\n\t  Server Msg Received : %s ",rmsg);
}
while((strcmp(rmsg,"exit"))!=0);
close(sd);
}

Client :2  program

 #include<stdio.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
int sd,len,cport;
struct sockaddr_in ser;
char smsg[20],rmsg[20];
printf("\n\t Enter the Port : ");
scanf("%d",&cport);
if((sd=socket(AF_INET,SOCK_STREAM,0))<0)
{
  printf("\n\t Error in socket");
  return 0;
}

bzero(&ser,sizeof(ser));
ser.sin_family=AF_INET;
ser.sin_port=htons(cport);
ser.sin_addr.s_addr=htonl(INADDR_ANY);

if(connect(sd,(struct sockaddr*)&ser,sizeof(ser)) <0)
{
  printf("\n\t Error in connect");
  exit(-1);
}

do
{

 printf("\n\t Enter the Client String :");
 scanf("%s",smsg);
 send(sd,smsg,20,0);
 wait(20);
 recv(sd,rmsg,20,0);
 printf("\n\t  Server Msg Received : %s ",rmsg);
}
while((strcmp(rmsg,"exit"))!=0);
close(sd);
}


OUTPUT

SERVER

[11ca013@mcalinux network]$ cc 15mulser.c
[11ca013@mcalinux network]$ ./a.out

         Enter the Port : 3326

         Enter the client no to be communicate : 1
         Client 1 is connected
         Accepted
         Clinet Msg Received : hi
         Enter Server String : how_are_you
         Clinet Msg Received : i_am_fine
         Enter Server String : exit

         Enter the client no to be communicate : 2
         Client 2 is connected
         Accepted
         Clinet Msg Received : hi
         Enter Server String : how_are_you
         Clinet Msg Received : i_am_boss
         Enter Server String : exit

         Enter the client no to be communicate : 0
[11ca013@mcalinux network]$

CLIENT:1

[11ca013@mcalinux network]$ cc 15mulcli1.c
[11ca013@mcalinux network]$ ./a.out

         Enter the Port : 3326

         Enter the Client String :hi
          Server Msg Received : how_are_you
         Enter the Client String :i_am_fine
          Server Msg Received : exit

CLIENT:2

[11ca013@mcalinux network]$ cc 15mulcli2.c
[11ca013@mcalinux network]$  ./a.out

         Enter the Port : 3326

         Enter the Client String :hi

          Server Msg Received : how_are_you
         Enter the Client String :i_am_boss
          Server Msg Received : exit


0 Response to " MULTI USER CHAT USING SOCKETS"

Post a Comment



Your IP Address is:

Browser: