Search This Blog

Sunday, 6 July 2014

EX2: Write a program to get date and time from server to client using TCP socket



EX2:Write a program to get date and time from server to client using TCP socket

Algorithm:

Server:

1.      Create a socket with the socket() system call.
2.     Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine.
3.     Listen for connections with the listen() system call.
4.   Accept a connection with the accept() system call. This call typically blocks until a client connects with the server.
5.      Get the date & time using ctime() system call.
6.      Send and receive data using the read() and write() system calls.
7.      Terminate the connections using close() system call.

Client:

1.      Create a socket with the socket() system call.
2.   Connect the socket to the address of the server using the connect() system call.
3.      Send and receive data using the read() and write() system calls.
4.      Terminate the connections using close() system call.


Server Program:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
int main()
{
int sd,b,cd;
//char *msg;
char msg[100];
struct sockaddr_in caddr,saddr;
socklen_t clen=sizeof(caddr);
time_t tick;
sd=socket(AF_INET,SOCK_STREAM,0);
if(sd!=-1)
printf("\nsocket is created");
else
printf("\nsocket is not created");
saddr.sin_addr.s_addr=htons(INADDR_ANY);
saddr.sin_port=htons(1200);
b=bind(sd,(struct sockaddr*)&saddr,sizeof(saddr));
if(b==0)
printf("\nbinded to client");
else
printf("\nnot binded");
listen(sd,5);
//while(1);
//{
cd=accept(sd,(struct sockaddr*)&caddr,&clen);
tick=time(NULL);
printf("Sys day & time: %s",ctime(&tick));
strcpy(msg,(char*)ctime(&tick));
send(cd,msg,sizeof(msg),0);
//}
close(sd);
close(cd);
return 0;
}


Client Program:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
int main()
{
        int sd,c;
        char msg[50],sip[15];
        struct sockaddr_in caddr;
        struct hostent *he;
        printf("Enter the ip address: ");
        scanf("%s", sip);
        he= gethostbyname(sip);
        sd = socket(AF_INET,SOCK_STREAM,0);
        if(sd!=-1)
        {
                printf("Socket is created");
        }
        else
        {
                printf("socket is not created");
        }
        caddr.sin_family = AF_INET;
        caddr.sin_addr   = *((struct in_addr*)he->h_addr);
        caddr.sin_port   = htons(1200);
        c= connect(sd, (struct sockaddr*)&caddr,sizeof(caddr));
        if(c==0)
                printf("connected to server");
        else
                printf("not connected");

        recv(sd,msg,sizeof(msg),0);
        printf(" The day and time : %s",msg);
        close(sd);
        return 0;
}

OUTPUT:

SERVER:


[cse@tecnetserver anand]$ cc ex2ser.c
[cse@tecnetserver anand]$ cc ex2ser.c -o server
[cse@tecnetserver anand]$ ./server

 socket is created
 binded to client sys day &time:Fri Aug 13 10:49:38 2013
[cse@tecnetserver anand]$

CLIENT:
[cse@tecnetserver anand]$ cc ex2cli.c
[cse@tecnetserver anand]$ cc ex2cli.c -o client
[cse@tecnetserver anand]$ ./client

Enter the ip address172.16.33.254
SOCKET IS CREATED
connected to server
The day and time:Fri Aug 13 10:49:38 2013
[cse@tecnetserver anand]$

No comments:

Post a Comment