Tuesday, 31 August 2010

UDP echo

உட்ப் ஏசோ



CLIENT
#include
#include
int main(int argc,char *argv[])
{
int sid;
char buff[100],buff1[100];
struct sockaddr_in saddr;
sid=socket(AF_INET,SOCK_DGRAM,0);
bzero(&saddr,sizeof(saddr));
saddr.sin_family=AF_INET;
saddr.sin_port=htons(5000);
printf("\nEnter The String to Server:");
scanf("%s",buff);
sendto(sid,buff,sizeof(buff),0,(struct sockaddr *)&saddr,sizeof(saddr));
printf("\nEcho Message From Server:");
recvfrom(sid,buff1,sizeof(buff1),0,NULL,NULL);
printf("%s\n",buff1);
close(sid);
}
SERVER
#include
#include
#include
int main()
{
int sid,cid;
char buff[100];
struct sockaddr_in saddr,caddr;
socklen_t clen;
sid=socket(AF_INET,SOCK_DGRAM,0);
bzero(&saddr,sizeof(saddr));
saddr.sin_family=AF_INET;
saddr.sin_addr.s_addr=htonl(INADDR_ANY);
saddr.sin_port=htons(5000);
clen=sizeof(caddr);
bind(sid,(struct sockaddr *)&saddr,sizeof(saddr));
printf("\nServer is Reading From Client !!!\n");
recvfrom(sid,buff,sizeof(buff),0,(struct sockaddr *)&caddr,&clen);
//printf("%s",buff);
printf("\nServer is again Writing the same to Client !!!\n");
sendto(sid,buff,sizeof(buff),0,(struct sockaddr *)&caddr,sizeof(caddr));
close(sid);
}
OUTPUT:
Client Output:
unixlab@unixlab-laptop:~$ ./uec
Enter The String to Server:hello
Echo Message From Server:hello
unixlab@unixlab-laptop:~$
Server Output:
unixlab@unixlab-laptop:~$ ./uec
Server is Reading File Name From Client !!!
Server is again Writing the same to Client !!!
unixlab@unixlab-laptop:~$

0 comments:

Post a Comment