]> git.lizzy.rs Git - rudp.git/blob - simconn.c
Switch to CMake
[rudp.git] / simconn.c
1 /*
2  *
3  * Demo for RUDPSimConnnect(...)
4  *
5  */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include "rudp.h"
10 #include "platform_adpt.h"
11
12 RUDPSOCKET s;
13
14 void *recv_thread(void *p)
15 {
16         struct timeval tv = { 1, 0 };
17         RUDPSOCKCHNO rset[2];
18         int nr, rlt;
19         char line[1000];
20
21         while(1)
22         {
23                 nr = 0;
24                 RUDP_SET(s, 0, rset, nr);
25                 if((rlt = RUDPSelect(rset, &nr, NULL, NULL, NULL, NULL, &tv)) > 0)
26                 {
27                         int chno, len, rudp;
28
29                         if(RUDP_ISSET(s, rset, nr))
30                         {
31                                 len = RUDPRecv(s, &chno, line, 1000, 0);
32                                 rudp = 1;
33                         }
34                         if(len > 0)
35                         {
36                                 line[len] = '\0';
37                                 printf("%s: %s\n", rudp?"rudp":"udp", line);
38                         }
39                         else
40                         {
41                                 printf("Recv Error %d\n", len);
42                                 break;
43                         }
44                 }
45                 else if(rlt == 0)
46                 {
47                         printf(".");
48                         fflush(stdout);
49                 }
50                 else
51                 {
52                         printf("RUDPSelect: %d\n", rlt);
53                         break;
54                 }
55         }
56         return NULL;
57 }
58
59
60 int main(int argc, char *argv[])
61 {
62         struct sockaddr_in sai;
63         int i, connected = 0;
64
65         if(argc < 2) { printf("simconn remotehost [localport remoteport]\n"); return -1; }
66
67
68         PA_NetLibInit();
69         RUDPStart();
70        
71         s = RUDPSocket();
72         memset(&sai, 0, sizeof(sai));
73         sai.sin_family = AF_INET;
74         sai.sin_port = htons(argc>2?atoi(argv[2]):5001);
75         if(RUDPBind(s, (struct sockaddr*)&sai, sizeof(sai)) < 0)
76         {
77                 printf("bind to local port %d failed\n", ntohs(sai.sin_port));
78                 RUDPCleanup();
79                 return -1;
80         }
81         //i = 1;
82         //RUDPSetSockOpt(s, OPT_ADHOC, &i, sizeof(i));
83
84
85         if(argc > 3) sai.sin_port = htons(atoi(argv[3]));
86         sai.sin_addr.s_addr = inet_addr(argv[1]);
87         sleep(5);
88         printf("begin connection....\n");
89         if(RUDPSimConnect(s, (struct sockaddr*)&sai, 1, NULL, 0) == 0)
90                 connected = 1;
91
92
93         char line[1000];
94         if(connected)
95         {
96                 pthread_t thd;
97                 pthread_create(&thd, NULL, recv_thread, NULL);
98                 pthread_detach(thd);
99
100                 while(fgets(line, 1000, stdin))
101                 {
102                         int rlt, flag = RUDPSELECT_WRITABLE;
103                         struct timeval tv = { 0, 0 };
104                         rlt = RUDPSelectSock(s, 0, flag, &tv);
105                         if(rlt > 0)
106                         {
107                                 if( (rlt = RUDPSend(s, 0, line, strlen(line), 0)) < 0)
108                                 {
109                                         printf("RUDPSend: %d\n", rlt);
110                                         break;
111                                 }
112                         }
113                         else
114                         {
115                         }
116                 }
117         }
118         else
119                 printf("connect to %s failed\n", argv[1]);
120
121         RUDPClose(s);
122
123         printf("press Enter to terminate.");
124         fgets(line, 1000, stdin);
125         RUDPCleanup();
126         return 0;
127 }
128