-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCP_TimeServer.c
More file actions
38 lines (38 loc) · 773 Bytes
/
Copy pathTCP_TimeServer.c
File metadata and controls
38 lines (38 loc) · 773 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int listenfd,connfd;
time_t ticks;
char buf[80];
struct sockaddr_in sa;
listenfd=socket(AF_INET,SOCK_STREAM,0);
if(listenfd==-1)
{
perror("socket error");
exit(1);
}
bzero(&sa,sizeof(sa));
sa.sin_family=AF_INET;
sa.sin_port=htons(13);
sa.sin_addr.s_addr=htonl(INADDR_ANY);
bind(listenfd,(struct sockaddr *)&sa,sizeof(sa));
listen(listenfd,5);
for(;;)
{
connfd=accept(listenfd,NULL,NULL);
ticks=time(NULL);
snprintf(buf,sizeof(buf),"%.24s\r\n",ctime(&ticks));
printf("%s",buf);
if( write( connfd, buf, strlen( buf)) < 0)
{
fprintf( stderr, "write error\n");
}
close(connfd);
}
}