-
Notifications
You must be signed in to change notification settings - Fork 0
/
timecli.c
43 lines (32 loc) · 887 Bytes
/
timecli.c
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
39
40
41
42
43
#include "time.h"
void err_quit(char *);
int main(int argc, char const *argv[])
{
int sockfd, n;
char recvline[MAXLINE + 1];
struct sockaddr_in servaddr;
if(argc != 2)
err_quit("Usage: ./a.out <IPaddres>");
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) <0 )
err_quit("Socket error");
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(13);
if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
err_quit("Error converting ASCII IP address to binary format");
if(connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) <0 )
err_quit("connect error");
while((n = read(sockfd, recvline, MAXLINE)) > 0 ) {
recvline[n] = 0;
if(fputs(recvline, stdout) == EOF )
err_quit("fputs error");
}
if ( n < 0 )
err_quit("read error");
return 0;
}
void err_quit(char *msg)
{
printf("%s\n", msg);
exit(-1);
}