forked from gozfree/gear-lib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_libptcp.c
110 lines (99 loc) · 2.66 KB
/
test_libptcp.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*****************************************************************************
* Copyright (C) 2014-2015
* file: test_libptcp.c
* author: gozfree <[email protected]>
* created: 2015-08-10 00:23
* updated: 2015-08-10 00:23
*****************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include "libptcp.h"
static int server(const char *host, uint16_t port)
{
int len;
struct sockaddr_in si;
char buf[32] = {0};
ptcp_socket_t *ps = ptcp_socket();
if (ps == NULL) {
printf("error!\n");
return -1;
}
si.sin_family = AF_INET;
si.sin_addr.s_addr = host ? inet_addr(host) : INADDR_ANY;
si.sin_port = htons(port);
ptcp_bind(ps, (struct sockaddr*)&si, sizeof(si));
ptcp_listen(ps, 0);
while (1) {
memset(buf, 0, sizeof(buf));
len = ptcp_recv(ps, buf, sizeof(buf));
if (len > 0) {
printf("ptcp_recv len=%d, buf=%s\n", len, buf);
} else if (ptcp_is_closed(ps)) {
printf("ptcp is closed\n");
return -1;
} else if (EWOULDBLOCK == ptcp_get_error(ps)){
//printf("ptcp is error: %d\n", ptcp_get_error(ps));
usleep(100 * 1000);
}
}
return 0;
}
static int client(const char *host, uint16_t port)
{
int i, len;
char buf[128] = {0};
struct sockaddr_in si;
ptcp_socket_t *ps = ptcp_socket();
if (ps == NULL) {
printf("error!\n");
return -1;
}
si.sin_family = AF_INET;
si.sin_addr.s_addr = inet_addr(host);
si.sin_port = htons(port);
if (0 != ptcp_connect(ps, (struct sockaddr*)&si, sizeof(si))) {
printf("ptcp_connect failed!\n");
} else {
printf("ptcp_connect success\n");
}
sleep(1);
i = 1;
//for (i = 0; i < 100; i++) {
while (1) {
usleep(10 * 1000);
memset(buf, 0, sizeof(buf));
snprintf(buf, sizeof(buf), "client %d\n", i);
len = 0;
ptcp_send(ps, buf, strlen(buf));
printf("ptcp_send i=%d, len=%d, buf=%s\n", i, len, buf);
}
sleep(1);
ptcp_close(ps);
return 0;
}
int main(int argc, char **argv)
{
if (argc != 2) {
printf("./test -c / -s\n");
return 0;
}
if (!strcmp(argv[1], "-c"))
client("127.0.0.1", 2333);
//client("192.168.1.100", 2333);
if (!strcmp(argv[1], "-s"))
server("127.0.0.1", 2333);
while (1) {
sleep(1);
}
return 0;
}