Sunday, February 12, 2012

Udp ehco network lab


Udp ehco

Server.c
#include "stdio.h"
#include "sys/types.h"
#include "sys/socket.h"
#include "netinet/in.h"
#include "string.h"
#include "stdlib.h"
int main()
{
            int sfd,i=0,port=5666;
            char buf[30];
            unsigned int t;
            struct sockaddr_in ser,cliaddr;
            if(sfd=(socket(AF_INET,SOCK_DGRAM,0))==-1)
            {
                        printf(“\nSocket creation error”);
                        return(0);
            }
            bzero((char *)&ser,sizeof(ser));
            ser.sin_family=AF_INET;
            ser.sin_addr.s_addr=inet_addr(“192.168.0.200”);
            ser.sin_port=htons(port);
            if((b=bind(sfd,(struct sockaddr*)&ser, sizeof(ser))==-1);
            {
                        printf(“\nBinding error”);
                        return(0);
            }
            t=sizeof(cliaddr);
            while(1)
            {
                        r=recvfrom(sfd,buff,sizeof(buff),0,(struct sockaddr*)&cliaddr, &t);
                        puts(buff);
                        if(buff[i]==’*’)
                                    break;
            }
            close(sfd);
            return 0;
}


Client.c

#include "stdio.h"
#include "sys/types.h"
#include "sys/socket.h"
#include "netinet/in.h"
#include "string.h"
#include "stdlib.h"
int main()
{
            int sd,i=0,port=5666;
            char buff[30];
            struct sockaddr_in ser,cliaddr;
            if((sd=socket(AF_INET,SOCK_DGRAM,0))==-1);
            {
                        printf(“\nSocket creation error”);
                        return(0);
            }
            bzero((char *)&ser,sizeof(ser));
            ser.sin_family=AF_INET;
            ser.sin_addr.s_addr= inet_addr(“192.168.0.200”);
            ser.sin_port=htons(port);
            printf("\n Client:");
            while(1)
            {
                        gets(buff);
                        sendto(sd,buff,sizeof(buff),0,(struct sockaddr*)&ser,sizeof(ser));           
                        if(buff[i]==’*’)
                                    break;
            }
            close(sd);
            return 0;
}

No comments:

Post a Comment