-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclient.c
50 lines (40 loc) · 1013 Bytes
/
client.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
44
45
46
47
48
49
50
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SAMPLE_SERVER_CONN 10
int main(int argc, char **argv)
{
int ret;
int sock;
struct sockaddr_in remote;
socklen_t remote_len;
if (argc != 3) {
fprintf(stderr, "%s [ip] [port]\n", argv[0]);
return -1;
}
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("failed to socket\n");
return -1;
}
remote.sin_family = AF_INET;
remote.sin_addr.s_addr = inet_addr(argv[1]);
remote.sin_port = htons(atoi(argv[2]));
remote_len = sizeof(struct sockaddr_in);
ret = connect(sock, (struct sockaddr *)&remote, remote_len);
if (ret < 0) {
perror("failed to accept\n");
close(sock);
return -1;
}
printf("connect success %d\n", ret);
close(sock);
return 0;
}