forked from wangbojing/NtyCo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnty_client.cpp
More file actions
111 lines (75 loc) · 3.11 KB
/
nty_client.cpp
File metadata and controls
111 lines (75 loc) · 3.11 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
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
/*
* Author : WangBoJing , email : 1989wangbojing@gmail.com
*
* Copyright Statement:
* --------------------
* This software is protected by Copyright and the information contained
* herein is confidential. The software may not be copied and the information
* contained herein may not be used or disclosed except with the written
* permission of Author. (C) 2017
*
*
**** ***** *****
*** * ** ***
*** * * * **
* ** * * ** **
* ** * * ** *
* ** * ** ** *
* ** * *** **
* ** * *********** ***** ***** ** ****
* ** * ** ** ** ** ** **
* ** * ** ** * ** * **
* ** * ** * * ** ** **
* ** * ** ** * ** * **
* ** * ** * * ** ** **
* ** * ** ** * ** ** **
* ** * ** ** * ** ** **
* ** * ** * * ** ** **
* ** * ** ** * ** * ** **
* *** ** * * ** * ** **
* *** ** * ** * * ** **
* ** ** * ** ** * ** **
* ** ** * * ** * ** **
***** * **** * ***** ****
*
*
*****
****
*
*/
#include "nty_coroutine.h"
#include <arpa/inet.h>
#define NTY_SERVER_IPADDR "192.168.189.1"
#define NTY_SERVER_PORT 9096
int init_client(void) {
int clientfd = nty_socket(AF_INET, SOCK_STREAM, 0);
if (clientfd <= 0) {
printf("socket failed\n");
return -1;
}
struct sockaddr_in serveraddr = {0};
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(NTY_SERVER_PORT);
serveraddr.sin_addr.s_addr = inet_addr(NTY_SERVER_IPADDR);
int result = nty_connect(clientfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
if (result != 0) {
printf("connect failed\n");
return -2;
}
return clientfd;
}
void client(void *arg) {
int clientfd = init_client();
char *buffer = "ntyco_client\r\n";
while (1) {
int length = nty_send(clientfd, buffer, strlen(buffer), 0);
printf("echo length : %d\n", length);
sleep(1);
}
}
int main(int argc, char *argv[]) {
nty_coroutine *co = NULL;
nty_coroutine_create(&co, client, NULL);
nty_schedule_run(); //run
return 0;
}