DISPLAYING DATE AND TIME USING TCP
By karthick
, in
DISPLAYING DATE AND TIME USING TCP
,
0 Comments
Server program
#include
<stdio.h>
#include
<sys/types.h>
#include
<netinet/in.h>
#include
<string.h>
#include
<sys/socket.h>
#include
<stdlib.h>
#include
<unistd.h>
#include
<time.h>
int
main()
{
int
sd,i,len,bi,nsd,port;
char
content[30],buff[30],last_received[30];
struct
sockaddr_in ser,cli;
if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1)
{
printf("\n
Socket problem");
return
0;
}
printf("\n
Socket created\n");
bzero((char*)&cli,sizeof(ser));
printf("ENTER
PORT NO:\n");
scanf("%d",&port);
printf("\n
Port Address is %d\n:",port);
ser.sin_family=AF_INET;
ser.sin_port=htons(port);
ser.sin_addr.s_addr=htonl(INADDR_ANY);
bi=bind(sd,(struct
sockaddr *)&ser,sizeof(ser));
if(bi==-1)
{
printf("\n
bind error, port busy, plz change port in client and server"
return
0;
}
i=sizeof(cli);
listen(sd,5);
nsd
= accept(sd,((struct sockaddr *)&cli),&i);
if(nsd==-1)
{
printf("\ncheck
the description parameter\n");
return
0;
}
printf("\nConnection
accepted");
time_t
ticks;
ticks=time(NULL);
snprintf(buff,sizeof(buff),"%24s\r\n",ctime(&ticks));
send(nsd,buff,30,0);
printf("\n
Bye..");
send(nsd,"EOF",4,0);
close(sd);
close(nsd);
return
0;
}
Client program
#include
<stdio.h>
#include
<sys/types.h>
#include
<netinet/in.h>
#include
<string.h>
#include
<sys/socket.h>
#include
<stdlib.h>
#include
<unistd.h>
int
main()
{
int
sd,con,port,i;
char
content[30];
struct
sockaddr_in cli;
if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1)
{
printf("\n
Socket problem");
return
0;
}
bzero((char*)&cli,sizeof(cli));
cli.sin_family
= AF_INET;
printf("ENTER
PORT NO");
scanf("%d",&port);
cli.sin_port=htons(port);
cli.sin_addr.s_addr=htonl(INADDR_ANY);
con=connect(sd,(struct
sockaddr*)&cli,sizeof(cli));
if(con==-1)
{
printf("\n
connection error");
return
0;
}
i=recv(sd,content,30,0);
printf("Received
from server %s\n",content);
close(sd);
return
0;
}
OUTPUT
SERVER
[11ca013@mcalinux
network]$ cc 3tcpser.c
[11ca013@mcalinux
network]$ ./a.out
Socket created
ENTER
PORT NO: 8596
Port Address is 8596
Connection
accepted
Bye..
CLIENT
[11ca013@mcalinux
network]$ cc 3tcpcli.c
[11ca013@mcalinux
network]$ ./a.out
ENTER
PORT NO8596
Received
from server Thu Feb 14 14:03:24 2013
0 Response to "DISPLAYING DATE AND TIME USING TCP"
Post a Comment