]> git.lizzy.rs Git - rudp.git/blob - simulconn.c
Switch to CMake
[rudp.git] / simulconn.c
1 /* 
2  * In this demo, two peers create(does not listen) 
3  * a rudp socket and connect to each other, simultaneously.
4  *
5  */
6 #include "rudp.h"
7 #include <string.h>
8 #include <stdio.h>
9 #include "platform_adpt.h"
10
11 RUDPSOCKET s;
12 int     udp_sock;
13
14 PA_THREAD_RETTYPE __STDCALL 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_FD_SET(udp_sock, rset, nr);
25                 RUDP_SET(s, 0, rset, nr);
26                 if((rlt = RUDPSelect(rset, &nr, NULL, NULL, NULL, NULL, &tv)) > 0)
27                 {
28                         int chno, len, rudp;
29
30                         if(RUDP_ISSET(s, rset, nr))
31                         {
32                                 len = RUDPRecv(s, &chno, line, 1000, 0);
33                                 rudp = 1;
34                         }
35                         else if(RUDP_FD_ISSET(udp_sock, rset, nr))
36                         {
37                                 struct sockaddr sa;
38                                 int sa_len = sizeof(sa);
39                                 len = recvfrom(udp_sock, line, 1000, 0, &sa, &sa_len);
40                                 rudp = 0;
41                         }
42                         if(len > 0)
43                         {
44                                 line[len] = '\0';
45                                 printf("%s: %s\n", rudp?"rudp":"udp", line);
46                         }
47                         else
48                         {
49                                 printf("Recv Error %d\n", len);
50                                 break;
51                         }
52                 }
53                 else if(rlt == 0)
54                 {
55                         printf(".");
56                         fflush(stdout);
57                 }
58                 else
59                 {
60                         printf("RUDPSelect: %d\n", rlt);
61                         break;
62                 }
63         }
64         return (PA_THREAD_RETTYPE)0;
65 }
66
67 int main(int argc, char *argv[])
68 {
69         struct sockaddr_in sai;
70         int i, connected = 0;
71         char line[1000];
72
73         if(argc < 2) { printf("simulconn remotehost [localport remoteport]\n"); return -1; }
74
75
76         PA_NetLibInit();
77         RUDPStart();
78        
79         s = RUDPSocket();
80         memset(&sai, 0, sizeof(sai));
81         sai.sin_family = AF_INET;
82         sai.sin_port = htons(argc>2?atoi(argv[2]):5001);
83         if(RUDPBind(s, (struct sockaddr*)&sai, sizeof(sai)) < 0)
84         {
85                 printf("bind to local port %d failed\n", ntohs(sai.sin_port));
86                 RUDPCleanup();
87                 return -1;
88         }
89         i = 1;
90         //RUDPSetSockOpt(s, OPT_ADHOC, &i, sizeof(i));
91
92         sai.sin_port = htons(ntohs(sai.sin_port)+1);
93         udp_sock = socket(AF_INET, SOCK_DGRAM, 0);
94         if(bind(udp_sock, (struct sockaddr*)&sai, sizeof(sai)) < 0)
95         {
96                 perror("bind udp");
97                 RUDPCleanup();
98                 return -1;
99         }
100
101
102         if(argc > 3) sai.sin_port = htons(atoi(argv[3]));
103         sai.sin_addr.s_addr = inet_addr(argv[1]);
104         PA_Sleep(5000);
105         printf("begin connection....\n");
106         for(i=0; i<3; i++)
107         {
108                 if(RUDPConnect(s, (struct sockaddr*)&sai, sizeof(sai)) == 0)
109                 {
110                         connected = 1;
111                         break;
112                 }
113                 else
114                 {
115                         printf("connection trying %d\n", i);
116                         PA_Sleep(1000);
117                 }
118         }
119
120         sai.sin_port = htons(ntohs(sai.sin_port)+1);
121
122         if(connected)
123         {
124                 PA_HTHREAD thd;
125                 thd = PA_ThreadCreate(recv_thread, NULL);
126
127                 while(fgets(line, 1000, stdin))
128                 {
129                         int rlt, flag = RUDPSELECT_WRITABLE;
130                         struct timeval tv = { 0, 0 };
131                         rlt = RUDPSelectSock(s, 0, flag, &tv);
132                         if(rlt > 0)
133                         {
134                                 if(line[0] & 0x01)
135                                 {
136                                         if( (rlt = RUDPSend(s, 0, line, strlen(line), 0)) < 0)
137                                         {
138                                                 printf("RUDPSend: %d\n", rlt);
139                                                 break;
140                                         }
141                                 }
142                                 else
143                                         sendto(udp_sock, line, strlen(line), 0, (struct sockaddr*)&sai, sizeof(sai));
144                         }
145                         else
146                         {
147                         }
148                 }
149         }
150         else
151                 printf("connect to %s failed\n", argv[1]);
152
153         RUDPClose(s);
154
155         printf("press Enter to terminate.");
156         fgets(line, 1000, stdin);
157         RUDPCleanup();
158
159         return 0;
160 }