質問編集履歴
1
実行結果追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
-
単純なUDPのサーバーを実装したくてC言語でコードを途中まで書きました。
|
1
|
+
単純なUDPのサーバー,クライアントを実装したくてC言語でコードを途中まで書きました。clientの方でACKの値が戻るようにしたいですが、ここからどうコードを書けばいいかわかりません。具体的なコードを教えて下さい。
|
2
|
-
|
3
|
-
|
4
|
-
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
-
```
|
5
|
+
```udpserver
|
6
|
+
|
7
|
+
//udpserver.c
|
6
8
|
|
7
9
|
#include <stdio.h>
|
8
10
|
|
@@ -152,6 +154,8 @@
|
|
152
154
|
|
153
155
|
*/
|
154
156
|
|
157
|
+
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *their_addr, socklen_t addrlen);
|
158
|
+
|
155
159
|
|
156
160
|
|
157
161
|
freeaddrinfo(servinfo);
|
@@ -163,3 +167,177 @@
|
|
163
167
|
}
|
164
168
|
|
165
169
|
```
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
```udpclient
|
174
|
+
|
175
|
+
//udpclient.c
|
176
|
+
|
177
|
+
#include <stdio.h>
|
178
|
+
|
179
|
+
#include <stdlib.h>
|
180
|
+
|
181
|
+
#include <unistd.h>
|
182
|
+
|
183
|
+
#include <errno.h>
|
184
|
+
|
185
|
+
#include <string.h>
|
186
|
+
|
187
|
+
#include <sys/types.h>
|
188
|
+
|
189
|
+
#include <sys/socket.h>
|
190
|
+
|
191
|
+
#include <netinet/in.h>
|
192
|
+
|
193
|
+
#include <arpa/inet.h>
|
194
|
+
|
195
|
+
#include <netdb.h>
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
#define SERVERPORT "4567" // the port that client will be connecting to
|
200
|
+
|
201
|
+
#define MAXBUFLEN 100
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
int main(int argc, char *argv[])
|
206
|
+
|
207
|
+
{
|
208
|
+
|
209
|
+
int sockfd;
|
210
|
+
|
211
|
+
struct addrinfo hints, *servinfo, *p;
|
212
|
+
|
213
|
+
struct sockaddr_storage their_addr;
|
214
|
+
|
215
|
+
socklen_t addr_len;
|
216
|
+
|
217
|
+
int rv;
|
218
|
+
|
219
|
+
int numbytes;
|
220
|
+
|
221
|
+
char buf[MAXBUFLEN];
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
if (argc != 3) {
|
226
|
+
|
227
|
+
fprintf(stderr,"usage: talker hostname message\n");
|
228
|
+
|
229
|
+
exit(1);
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
memset(&hints, 0, sizeof hints);
|
236
|
+
|
237
|
+
hints.ai_family = AF_UNSPEC;
|
238
|
+
|
239
|
+
hints.ai_socktype = SOCK_DGRAM;
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
if ((rv = getaddrinfo(argv[1], SERVERPORT, &hints, &servinfo)) != 0) {
|
244
|
+
|
245
|
+
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
|
246
|
+
|
247
|
+
return 1;
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
// loop through all the results and make a socket
|
252
|
+
|
253
|
+
for(p = servinfo; p != NULL; p = p->ai_next) {
|
254
|
+
|
255
|
+
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
|
256
|
+
|
257
|
+
perror("talker: socket");
|
258
|
+
|
259
|
+
continue;
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
break;
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
if (p == NULL) {
|
268
|
+
|
269
|
+
fprintf(stderr, "talker: failed to create socket\n");
|
270
|
+
|
271
|
+
return 2;
|
272
|
+
|
273
|
+
}
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
// Send to server
|
278
|
+
|
279
|
+
if ((numbytes = sendto(sockfd, argv[2], strlen(argv[2]), 0, p->ai_addr, p->ai_addrlen)) == -1) {
|
280
|
+
|
281
|
+
perror("client: sendto");
|
282
|
+
|
283
|
+
exit(1);
|
284
|
+
|
285
|
+
}
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
//Receive from server
|
290
|
+
|
291
|
+
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const
|
292
|
+
|
293
|
+
struct sockaddr *dest_addr, socklen_t addrlen);
|
294
|
+
|
295
|
+
/*
|
296
|
+
|
297
|
+
Complete this part
|
298
|
+
|
299
|
+
*/
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
printf("Received from server: %s\n", buf);
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
freeaddrinfo(servinfo);
|
308
|
+
|
309
|
+
close(sockfd);
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
return 0;
|
314
|
+
|
315
|
+
}
|
316
|
+
|
317
|
+
```
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
現在の実行結果
|
322
|
+
|
323
|
+
```terminal
|
324
|
+
|
325
|
+
gcc udpserver.c
|
326
|
+
|
327
|
+
./a.out
|
328
|
+
|
329
|
+
server: waiting for client...
|
330
|
+
|
331
|
+
Received from client: Hello
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
gcc udpclient.c
|
338
|
+
|
339
|
+
./a.out 127.0.0.1 Hello
|
340
|
+
|
341
|
+
Received from server: �*��z```
|
342
|
+
|
343
|
+
```
|