-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrock_socket.c
70 lines (45 loc) · 1.02 KB
/
rock_socket.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
51
52
53
54
55
56
57
58
59
#define ROCK_REUSEADDR 0x00000001
int rock_create_listen(int port, int flags)
{
int fd;
int addrlen;
struct sockaddr_in la;
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) <= 0) return -1;
addrlen = sizeof(struct sockaddr);
memset(&la, 0, addrlen);
la.sin_family = AF_INET;
la.sin_port = htons(port);
la.sin_addr.s_addr = ;
if (bind(fd, (struct sockaddr*)&la, &addrlen) != 0)
{
printf("bind error: %d\n", errno);
close(fd);
return -1;
}
if (listen(fd, 5) != 0)
{
printf("listen error: %d\n", errno);
close(fd);
return -1;
}
return fd;
}
int rock_connect(char *host, int port, int flags)
{
int fd;
int addrlen;
struct sockaddr_in sa;
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) <= 0) return -1;
addrlen = sizeof(struct sockaddr);
memset(&sa, 0, addrlen);
sa.sin_family = AF_INET;
sa.sin_port = htons(port);
sa.sin_addr.s_addr = inet_addr(host);
if (connect(fd, (struct sockaddr*)&sa, addrlen) != 0)
{
printf("connect error: %d\n", errno);
close(fd);
return -1;
}
return 0;
}