Echo server
#include<unistd.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
#include<sys/socket.h>
main()
{
int sd,nsd,i=0,port=6700;
unsigned int t;
char content[30];
struct sockaddr_in ser,cli;
if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1)
{
printf("Error:Socket creation in
Server\n");
return 0;
}
bzero((char*)&ser,sizeof(ser));
printf("The port address is %d
\n",port);
ser.sin_family=AF_INET;
ser.sin_port=htons(port);
ser.sin_addr.s_addr=inet_addr("192.168.0.200");
if(bind(sd,(struct
sockaddr*)&ser,sizeof(ser))==-1)
{
printf("Error: Binding problem
port busy");
return 0;
}
listen(sd,1);
printf("\tserver\n");
printf("************************************************************\n");
t=sizeof(cli);
nsd=accept(sd,(struct
sockaddr*)&cli,&t);
while(1)
{
read(nsd,&content[i],1);
write(nsd,&content[i],1);
if(content[i++]=='*')
break;
}
close(nsd);
close(sd);
return 0;
}
Server o/p:
[cs2910007@localhost
netlab]$ cc echoserver.c
[cs2910007@localhost
netlab]$ ./a.out
The port address is
6700
server
************************************************************
[cs2910007@localhost
netlab]$
Echo client
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
#include<sys/socket.h>
main()
{
int
sd,i=0,port=6700;
char
content[30]="\0";
char
cont[30]="\0";
struct sockaddr_in ser,cli;
if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1)
{
printf("Error:Socket creation in server\n");
return 0;
}
bzero((char *)&ser,sizeof(ser));
printf("The
port address is %d \n",port);
ser.sin_family=AF_INET;
ser.sin_port=htons(port);
ser.sin_addr.s_addr=inet_addr("192.168.0.200");
if(connect(sd,(struct sockaddr *)&ser,sizeof(ser))==-1)
{
printf("Error in connecting port
busy");
return 0;
}
printf("Enter the
data \n");
while((content[i]=(char)getchar())!='*')
{
write(sd,&content[i],1);
read(sd,&cont[i],1);
putchar((int)cont[i]);
}
close(sd);
return 0;
}
Client o/p:
[cs2910007@localhost
netlab]$ cc echoclient.c
[cs2910007@localhost
netlab]$ ./a.out
The port address is
6700
Enter the data
695
695
kcet
kcet
kamarajcse
kamarajcse
hello
hello
*
[cs2910007@localhost
netlab]$
No comments:
Post a Comment