tcp client
server :
Server
#include<unistd.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
#include<sys/socket.h>
int main()
{
int
sd,nsd,i=0,port=7996;
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("Socket
was created\n");
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("\t\tServer\n");
t=sizeof(cli);
nsd=accept(sd,(struct sockaddr *)&cli,&t);
if(nsd==-1)
{
printf("Error in accepting the sever");
return 0;
}
printf("client accepted\n");
while(1)
{
recv(nsd,content,50,0);
puts(content);
gets(content);
send(nsd,content,10,0);
printf("%c",content[i]);
if(content[i]=='*')
break;
}
close(nsd);
close(sd);
return 0;
}
Server o/p:
The port address is 7996
Server
client accepted
5
6
6
*
[cs2910007@localhost
netlab]$
Client
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
#include<sys/socket.h>
int main()
{
int
sd,i=0,port=7996;
char
content[30]="\0";
struct
sockaddr_in ser;
printf("Client
side\n");
if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1)
{
printf("Error with the Socket creation in server\n");
return 0;
}
bzero((char *)&ser,sizeof(ser));
printf("The port address is with
number %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("This is client");
printf("Enter any data :\n");
while(1)
{
gets(content);
send(sd,content,10,0);
recv(sd,content,10,0);
puts(content);
if(content[i]=='*')
break;
}
close(sd);
return 0;
}
Client o/p:
[cs2910007@localhost
netlab]$ ./a.out
Client side
The port address is with
number 7996
This is clientEnter any
data :
5
6
9
*
[cs2910007@localhost
netlab]$
No comments:
Post a Comment