Computer Network: Server and Client (Socket Programming) Simple chatting program in C Programming

Posted on:
tags: , ,

SERVER


#include<stdio.h>
#include<netdb.h>
#include<string.h>
int main()
{
int sd,sd1,l;
char ch[30],ch1[30];
struct sockaddr_in s,c;
s.sin_family=AF_INET;
s.sin_addr.s_addr=inet_addr("192.168.6.25");
s.sin_port=htons(6001);
bzero(&(s.sin_zero),8);
sd=socket(AF_INET,SOCK_STREAM, 0);
bind(sd,(struct sockaddr*)&s, sizeof(s));
listen(sd,5);
sd1=accept(sd,(struct sockaddr*)&c,&l);
while(1)
{
read(sd1,ch,30);
int d=strcmp(ch,"q");
if(d==0)
break;
printf("%s",ch);
printf("\n");
printf("Enter the data:");
gets(ch1);
write(sd1,ch1,30);
}
printf("thank you...");
close(sd1);
return(0);
}


CLIENT



#include<netdb.h>
int main()
{
int sd2;
        char ch[30],ch1[30];
        struct sockaddr_in s,c;
        s.sin_family=AF_INET;
        s.sin_addr.s_addr=inet_addr("192.168.6.25");
        s.sin_port=htons(6001);
        bzero(&(s.sin_zero),8);
sd2=socket(AF_INET,SOCK_STREAM,0);
connect(sd2,(struct sockaddr*)&s,sizeof(s));
while(1)
{
printf("\n");
printf("Enter the data:");
gets(ch);
write(sd2,ch,30);
printf("\n");
read(sd2,ch1,30);
printf("%s",ch1);
}
close(sd2);
return(0);
}


No comments:

Post a comment

< >