-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatetime-client.c
More file actions
73 lines (56 loc) · 1.63 KB
/
Copy pathdatetime-client.c
File metadata and controls
73 lines (56 loc) · 1.63 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#define BUFFSIZE 2048
int main(int argc, char *argv[]){
int INET_PROTOCL, sock, s, n;
char destip[100], destport[100], buf[BUFFSIZE];
if(argc < 2){
printf("Please specify dest address.\n");
return 0;
}
strcpy(destip, argv[1]);
printf("Dest address: %s\n", destip);
strcpy(destport, "13");
printf("Dest port number: %s\n", destport);
char *p = strrchr(destip, ':');
if(p == NULL){
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(atoi(destport));
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("Socket failed.\n");
return 0;
}
s = inet_pton(AF_INET, destip, &server.sin_addr);
connect(sock, (struct sockaddr *)&server,
sizeof(server));
}
else {
struct sockaddr_in6 server;
server.sin6_family = AF_INET6;
server.sin6_port = htons(atoi(destport));
sock = socket(AF_INET6, SOCK_STREAM, 0);
if (sock < 0) {
perror("Socket failed.\n");
return 0;
}
s = inet_pton(AF_INET6, destip, &server.sin6_addr);
connect(sock, (struct sockaddr *)&server,
sizeof(server));
}
memset(buf, 0, sizeof(buf));
n = read(sock, buf, sizeof(buf));
if(n < 0){
perror("Error reading from socket.\n");
return 0;
}
printf("\n%s\n", buf);
}