質問編集履歴
4
書式の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,10 +6,12 @@
|
|
6
6
|
|
7
7
|
回答お願いします。
|
8
8
|
|
9
|
+
|
10
|
+
|
11
|
+
追記 tcpを使用したチャットプログラムを書きました 参考にして修正してもらいたいです
|
12
|
+
|
9
13
|
```C言語
|
10
14
|
|
11
|
-
コード
|
12
|
-
|
13
15
|
#include <stdio.h>
|
14
16
|
|
15
17
|
#include <stdlib.h>
|
@@ -434,4 +436,6 @@
|
|
434
436
|
|
435
437
|
exit(0);
|
436
438
|
|
439
|
+
}
|
440
|
+
|
437
|
-
|
441
|
+
```
|
3
ふ
test
CHANGED
File without changes
|
test
CHANGED
@@ -434,4 +434,4 @@
|
|
434
434
|
|
435
435
|
exit(0);
|
436
436
|
|
437
|
-
}
|
437
|
+
}```
|
2
プログラム
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,7 +6,9 @@
|
|
6
6
|
|
7
7
|
回答お願いします。
|
8
8
|
|
9
|
-
|
9
|
+
```C言語
|
10
|
+
|
11
|
+
コード
|
10
12
|
|
11
13
|
#include <stdio.h>
|
12
14
|
|
1
tcpを使いチャットをできるようにしたプログラムを追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -5,3 +5,431 @@
|
|
5
5
|
1~9の数字を打つと九九を表示する簡単なプログラムは出来るのですが、これをクライアントとサーバ間でやる方法がわかりません。
|
6
6
|
|
7
7
|
回答お願いします。
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
#include <stdio.h>
|
12
|
+
|
13
|
+
#include <stdlib.h>
|
14
|
+
|
15
|
+
#include <string.h>
|
16
|
+
|
17
|
+
#include <unistd.h>
|
18
|
+
|
19
|
+
#include <netdb.h>
|
20
|
+
|
21
|
+
#include <sys/socket.h>
|
22
|
+
|
23
|
+
#include <arpa/inet.h>
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
#define PORT 20007
|
28
|
+
|
29
|
+
#define BUF_SIZE 0x1000
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
static void server(int port){
|
34
|
+
|
35
|
+
struct sockaddr_in sin;
|
36
|
+
|
37
|
+
fd_set rfds;
|
38
|
+
|
39
|
+
int ld, sd[5], len, max,x,y;
|
40
|
+
|
41
|
+
char *buf, str[256];
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
if ((buf = malloc(BUF_SIZE)) == NULL) {
|
46
|
+
|
47
|
+
perror("malloc");
|
48
|
+
|
49
|
+
return;
|
50
|
+
|
51
|
+
}
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
for(x=0;x<5;x++) sd[x] = -1;
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
if ((ld = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
|
60
|
+
|
61
|
+
perror("socket");
|
62
|
+
|
63
|
+
goto close_and_end;
|
64
|
+
|
65
|
+
}
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
memset(&sin, 0, sizeof(sin));
|
70
|
+
|
71
|
+
sin.sin_family = AF_INET;
|
72
|
+
|
73
|
+
sin.sin_port = htons(port);
|
74
|
+
|
75
|
+
if (bind(ld, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
|
76
|
+
|
77
|
+
perror("bind");
|
78
|
+
|
79
|
+
goto close_and_end;
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
if (listen(ld, 3) < 0) {
|
84
|
+
|
85
|
+
perror("listen");
|
86
|
+
|
87
|
+
goto close_and_end;
|
88
|
+
|
89
|
+
}
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
loop:
|
94
|
+
|
95
|
+
FD_ZERO(&rfds);
|
96
|
+
|
97
|
+
FD_SET(ld, &rfds);
|
98
|
+
|
99
|
+
max = ld;
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
for(x=0;x<5;x++){
|
104
|
+
|
105
|
+
if (sd[x] >= 0) {
|
106
|
+
|
107
|
+
FD_SET(sd[x], &rfds);
|
108
|
+
|
109
|
+
if (max < sd[x]) max = sd[x];
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
select(max + 1, &rfds, NULL, NULL, NULL);
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
for(x=0;x<5;x++){
|
122
|
+
|
123
|
+
if (sd[x] > 0 && FD_ISSET(sd[x], &rfds)) {
|
124
|
+
|
125
|
+
if ((len = read(sd[x], buf, BUF_SIZE)) > 0) {
|
126
|
+
|
127
|
+
if (write(sd[x], buf, len) < 0) {
|
128
|
+
|
129
|
+
close(sd[x]);
|
130
|
+
|
131
|
+
sd[x] = -1;
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
for(y=0;y<5;y++){
|
136
|
+
|
137
|
+
if(x!=y){
|
138
|
+
|
139
|
+
if (sd[y] > 0) {
|
140
|
+
|
141
|
+
if (write(sd[y], buf, len) < 0) {
|
142
|
+
|
143
|
+
close(sd[y]);
|
144
|
+
|
145
|
+
sd[y] = -1;
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
else {
|
158
|
+
|
159
|
+
if (len < 0) perror("read");
|
160
|
+
|
161
|
+
close(sd[x]);
|
162
|
+
|
163
|
+
sd[x] = -1;
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
if (FD_ISSET(ld, &rfds)) {
|
174
|
+
|
175
|
+
int tmpd;
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
len = sizeof(sin);
|
180
|
+
|
181
|
+
if ((tmpd = accept(ld, (struct sockaddr*)&sin, (socklen_t*)&len))
|
182
|
+
|
183
|
+
< 0) {
|
184
|
+
|
185
|
+
perror("accept");
|
186
|
+
|
187
|
+
goto close_and_end;
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
inet_ntop(AF_INET, &sin.sin_addr.s_addr, str, sizeof (str));
|
192
|
+
|
193
|
+
printf("connected from %s\n", str);
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
if(sd[0]<0)
|
198
|
+
|
199
|
+
sd[0]=tmpd;
|
200
|
+
|
201
|
+
else if(sd[1]<0)
|
202
|
+
|
203
|
+
sd[1]=tmpd;
|
204
|
+
|
205
|
+
else if(sd[2]<0)
|
206
|
+
|
207
|
+
sd[2]=tmpd;
|
208
|
+
|
209
|
+
else if(sd[3]<0)
|
210
|
+
|
211
|
+
sd[3]=tmpd;
|
212
|
+
|
213
|
+
else if(sd[4]<0)
|
214
|
+
|
215
|
+
sd[4]=tmpd;
|
216
|
+
|
217
|
+
else if(sd[5]<0)
|
218
|
+
|
219
|
+
sd[5]=tmpd;
|
220
|
+
|
221
|
+
else{
|
222
|
+
|
223
|
+
strcpy(buf, "Server too busy\n");
|
224
|
+
|
225
|
+
send(tmpd, buf, strlen(buf), 0);
|
226
|
+
|
227
|
+
close(tmpd);
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
goto loop;
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
close_and_end:
|
238
|
+
|
239
|
+
for(x=0;x<5;x++){
|
240
|
+
|
241
|
+
if (sd[x] >= 0) close(sd[x]);
|
242
|
+
|
243
|
+
if (ld >= 0) close(ld);
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
free(buf);
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
static void client(unsigned int ip, int port){
|
256
|
+
|
257
|
+
struct sockaddr_in sin;
|
258
|
+
|
259
|
+
char *buf;
|
260
|
+
|
261
|
+
int sd, len, i;
|
262
|
+
|
263
|
+
fd_set rfds;
|
264
|
+
|
265
|
+
int max;
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
if ((buf = malloc(BUF_SIZE)) == NULL) {
|
270
|
+
|
271
|
+
perror("malloc");
|
272
|
+
|
273
|
+
return;
|
274
|
+
|
275
|
+
}
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
if ((sd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
|
280
|
+
|
281
|
+
perror("socket");
|
282
|
+
|
283
|
+
goto close_and_end;
|
284
|
+
|
285
|
+
}
|
286
|
+
|
287
|
+
memset(&sin, 0, sizeof(sin));
|
288
|
+
|
289
|
+
sin.sin_family = AF_INET;
|
290
|
+
|
291
|
+
sin.sin_port = htons(port);
|
292
|
+
|
293
|
+
sin.sin_addr.s_addr = htonl(ip);
|
294
|
+
|
295
|
+
if (connect(sd, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
|
296
|
+
|
297
|
+
perror("connect");
|
298
|
+
|
299
|
+
goto close_and_end;
|
300
|
+
|
301
|
+
}
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
loop:
|
306
|
+
|
307
|
+
FD_ZERO(&rfds);
|
308
|
+
|
309
|
+
max=1;
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
if (0 < sd){
|
314
|
+
|
315
|
+
FD_SET(sd, &rfds);
|
316
|
+
|
317
|
+
if(max<sd) max = sd;
|
318
|
+
|
319
|
+
}
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
FD_SET(0, &rfds);
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
select(max + 1, &rfds, NULL, NULL, NULL);
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
if(FD_ISSET(0, &rfds)){
|
332
|
+
|
333
|
+
if (fgets(buf, BUF_SIZE, stdin) <= 0)
|
334
|
+
|
335
|
+
goto close_and_end;
|
336
|
+
|
337
|
+
len = strlen(buf);
|
338
|
+
|
339
|
+
if (send(sd, buf, len, 0) < 0) {
|
340
|
+
|
341
|
+
perror("send");
|
342
|
+
|
343
|
+
goto close_and_end;
|
344
|
+
|
345
|
+
}
|
346
|
+
|
347
|
+
}
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
if(FD_ISSET(sd, &rfds)){
|
352
|
+
|
353
|
+
len = recv(sd, buf, BUF_SIZE, 0);
|
354
|
+
|
355
|
+
if (len <= 0) {
|
356
|
+
|
357
|
+
if (len < 0) perror("send");
|
358
|
+
|
359
|
+
goto close_and_end;
|
360
|
+
|
361
|
+
}
|
362
|
+
|
363
|
+
for (i = 0; i < len; i++) putchar(buf[i]);
|
364
|
+
|
365
|
+
}
|
366
|
+
|
367
|
+
goto loop;
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
close_and_end:
|
372
|
+
|
373
|
+
if (sd >= STDIN_FILENO) close(sd);
|
374
|
+
|
375
|
+
if (sd >= 0) close(sd);
|
376
|
+
|
377
|
+
free(buf);
|
378
|
+
|
379
|
+
}
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
static void usage(void){
|
384
|
+
|
385
|
+
printf("server mode: tcp2 -s\n");
|
386
|
+
|
387
|
+
printf("client mode: tcp2 -c\n");
|
388
|
+
|
389
|
+
exit(0);
|
390
|
+
|
391
|
+
}
|
392
|
+
|
393
|
+
|
394
|
+
|
395
|
+
int main(int argc, char **argv, char **env){
|
396
|
+
|
397
|
+
if (argc != 2) usage();
|
398
|
+
|
399
|
+
if (argv[1][0] != '-') usage();
|
400
|
+
|
401
|
+
switch (argv[1][1]) {
|
402
|
+
|
403
|
+
case 's':
|
404
|
+
|
405
|
+
server(PORT);
|
406
|
+
|
407
|
+
break;
|
408
|
+
|
409
|
+
case 'c':
|
410
|
+
|
411
|
+
{
|
412
|
+
|
413
|
+
struct hostent *ent;
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
ent = gethostbyname("localhost");
|
418
|
+
|
419
|
+
client(ntohl(*(int*)ent->h_addr_list[0]), PORT);
|
420
|
+
|
421
|
+
break;
|
422
|
+
|
423
|
+
}
|
424
|
+
|
425
|
+
default:
|
426
|
+
|
427
|
+
usage();
|
428
|
+
|
429
|
+
}
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
exit(0);
|
434
|
+
|
435
|
+
}
|