Translate

Monday, October 8, 2012

ME NETWORKS LAB-->multi user chat


multi user chat

 

Server

 

#include<stdio.h>

#include<sys/types.h>

#include<netinet/in.h>

#include<string.h>

 

main()

{

int i,sd,sd2,nsd,clilen,sport,len;

char sendmsg[20],rcvmsg[20];

struct sockaddr_in servaddr,cliaddr;

scanf("%d",&sport);

sd=socket(AF_INET,SOCK_STREAM,0);

if(sd<0)

        printf("Can't Create \n");

else

        printf("Socket is Created\n");

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=htonl(INADDR_ANY);

servaddr.sin_port=htons(sport);

sd2=bind(sd,(struct sockaddr*)&servaddr,sizeof(servaddr));

if(sd2<0)

        printf("Can't Bind\n");

else

        printf("\n Binded\n");

 

listen(sd,5);

do

{

printf("Enter the client no to communicate\n");

scanf("%d",&i);

if(i==0)

        exit(0);

printf("Client %d is connected\n",i);

clilen=sizeof(cliaddr);

nsd=accept(sd,(struct sockaddr*)&cliaddr,&clilen);

if(nsd<0)

        printf("Can't Accept\n");

else

        printf("Accepted\n");

 

 

do

        {

        recv(nsd,rcvmsg,20,0);

        printf("%s",rcvmsg);

        fgets(sendmsg,20,stdin);

        len=strlen(sendmsg);

        sendmsg[len-1]='\0';

        send(nsd,sendmsg,20,0);

        wait(20);

        }while(strcmp(sendmsg,"bye")!=0);

}while(i!=0);

}

client - 1


 

#include<stdio.h>

#include<sys/types.h>

#include<netinet/in.h>

main()

{

int csd,cport,len;

char sendmsg[20],revmsg[20];

struct sockaddr_in servaddr;

printf("Enter the port\n");

scanf("%d",&cport);

csd=socket(AF_INET,SOCK_STREAM,0);

if(csd<0)

        printf("Can't Create\n");

else

        printf("Socket is Created\n");

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=htonl(INADDR_ANY);

servaddr.sin_port=htons(cport);

if(connect(csd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)

        printf("Can't Connect\n");

else

        printf("Connected\n");

 

do

        {

fgets(sendmsg,20,stdin);

len=strlen(sendmsg);

sendmsg[len-1]='\0';

send(csd,sendmsg,20,0);

wait(20);

recv(csd,revmsg,20,0);

printf("%s",revmsg);

}

while(strcmp(revmsg,"bye")!=0);

}

 

client - 2

 

 

 

#include<stdio.h>

#include<sys/types.h>

#include<netinet/in.h>

main()

{

int csd,cport,len;

char sendmsg[20],revmsg[20];

struct sockaddr_in servaddr;

printf("Enter the port\n");

scanf("%d",&cport);

csd=socket(AF_INET,SOCK_STREAM,0);

if(csd<0)

        printf("Can't Create\n");

else

        printf("Socket is Created\n");

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=htonl(INADDR_ANY);

servaddr.sin_port=htons(cport);

if(connect(csd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)

        printf("Can't Connect\n");

else

        printf("Connected\n");

 

do

        {

fgets(sendmsg,20,stdin);

len=strlen(sendmsg);

sendmsg[len-1]='\0';

send(csd,sendmsg,20,0);

wait(20);

recv(csd,revmsg,20,0);

printf("%s",revmsg);

}

while(strcmp(revmsg,"bye")!=0);

}

 

 

output:

 

Server Side

 

[mecse8@localhost mecse8]$ ./m1

6523

Socket is Created

 

 Binded

Enter the client no to communicate

1

Client 1 is connected

Accepted

bye

 

bye

 

bye

Enter the client no to communicate

 

 

2

Client 2 is connected

Accepted

bye

bye

Enter the client no to communicate

3

Client 3 is connected

Accepted

bye

 

bye

 

bye

Enter the client no to communicate

0

 

Client Side - 1


 

[mecse8@localhost mecse8]$ ./c1

Enter the port

6523

Socket is Created

Connected

bye

 

bye

bye[mecse8@localhost mecse8]$

 

 

Client Side – 2

 

[mecse8@localhost mecse8]$ ./c2

Enter the port

6523

Socket is Created

Connected

bye

 

bye[mecse8@localhost mecse8]$

 

 

 

 

 

No comments:

Post a Comment