Tuesday, 5 April 2011

Write a simple UDP client-server program to print day,time and ipaddress of client by server.(server side )

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#include<stdlib.h>
int main(int argc,char **argv)
{
    struct sockaddr_in mysock,newsock;
    int sockfd;
    char msg[1024];
    int size,val;
    if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)
    {
        perror("no socket");
        exit(0);
    }
    size=sizeof(struct sockaddr);
    socklen_t len=sizeof(newsock);
    bzero(&mysock,size);
    mysock.sin_family=AF_INET;
    mysock.sin_port=htons(45000);
    mysock.sin_addr.s_addr=htonl(INADDR_ANY);
    if((bind(sockfd,(struct sockaddr*)&mysock,size))<0)
    {
        perror("no bind");
        exit(0);
    }


        if((val=recvfrom(sockfd,msg,1024,0,(struct sockaddr*)&newsock,&len))<0)
        {
            perror(" ");
            exit(0);
        }
        printf("\n Client's time is:");


        printf("%s",msg);


    close(sockfd);
    return 0;
}




OUTPUT:
Just run the server program after compilation .When  you will run the client program with server's ip address  then server will print day and time of client.




$cc server.c
$./a.out

0 comments

 
© 2011-2012 ProgrammingBlue
Posts RSS Comments RSS
Back to top