udp client server
UDP single server
#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,nsd,i=0,port=7052;
unsigned
int t;
char
content[30];
struct
sockaddr_in ser,cli;
if((sd=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))==-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);
}
printf("\t\tServer\n");
t=sizeof(cli);
printf("\n client accepted\n");
while(1)
{
recvfrom(sd,content,50,0,(struct sockaddr*)&cli,&t);
puts(content);
if(content[i]=='*')
break;
}
printf("\n");
close(sd);
return 0;
}
Server o/p:
[cs2910007@localhost
netlab]$ ./a.out
The port address is 7052
Server
client accepted
5
9
6
9
*
[cs2910007@localhost
netlab]$
UDP Client single
#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=7052;
char
cont[30]="\0";
struct
sockaddr_in ser;
if((sd=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))==-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("This is client");
printf("Enter any data :\n");
while(1)
{
gets(cont);
sendto(sd,cont,50,0,(struct sockaddr*)&ser,sizeof(ser));
if(cont[i]=='*')
break;
}
close(sd);
return 0;
}
Client o/p:
[cs2910007@localhost netlab]$
./a.out
The port address is 7052
This is clientEnter any data :
5
9
9
*
[cs2910007@localhost netlab]$
No comments:
Post a Comment