質問編集履歴
4
server側はおそらく動いています。
test
CHANGED
File without changes
|
test
CHANGED
@@ -335,3 +335,13 @@
|
|
335
335
|
|
336
336
|
|
337
337
|
初めて書くので見にくかったり書き方が下手だとは思うんですけどよろしくお願いします。
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
netstat -antのコマンドを実行するとserverプログラムが”LISTEN”状態になっているのでこちらは問題なく動作していると思います。ただAddressが見えないのでそのアドレスをクライアント側の引数に入れると動作すると思っています。
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
Proto Recv-Q Send-Q Local Address Foreign Address (state)
|
346
|
+
|
347
|
+
tcp4 0 0 *.19991 *.* LISTEN
|
3
すいません。おんなじになってました
test
CHANGED
File without changes
|
test
CHANGED
@@ -16,8 +16,228 @@
|
|
16
16
|
|
17
17
|
#include <sys/socket.h>
|
18
18
|
|
19
|
+
#include <sys/time.h>
|
20
|
+
|
21
|
+
#include <unistd.h>
|
22
|
+
|
19
23
|
#include <netinet/in.h>
|
20
24
|
|
25
|
+
|
26
|
+
|
27
|
+
#define PORT 19991
|
28
|
+
|
29
|
+
#define MAX_CLIENT 5
|
30
|
+
|
31
|
+
#define FREE 100
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
main()
|
38
|
+
|
39
|
+
{
|
40
|
+
|
41
|
+
int i, j, n, *server, *clients, sockets[MAX_CLIENT+2], len, p;
|
42
|
+
|
43
|
+
fd_set fds;
|
44
|
+
|
45
|
+
struct sockaddr_in saddr;
|
46
|
+
|
47
|
+
struct sockaddr_in caddr;
|
48
|
+
|
49
|
+
static char buf[100], x[100];
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
/* Intalize a socket ------------------------------------------ */
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
server = sockets;
|
60
|
+
|
61
|
+
clients = sockets+1;
|
62
|
+
|
63
|
+
if ((*server = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
64
|
+
|
65
|
+
perror("socket");
|
66
|
+
|
67
|
+
exit(1);
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
/* Bind the socket -------------------------------------------- */
|
72
|
+
|
73
|
+
saddr.sin_family = AF_INET;
|
74
|
+
|
75
|
+
saddr.sin_addr.s_addr = INADDR_ANY;
|
76
|
+
|
77
|
+
saddr.sin_port = htons(PORT);
|
78
|
+
|
79
|
+
if (bind(*server,(struct sockaddr *)&saddr,sizeof(struct sockaddr_in))<0) {
|
80
|
+
|
81
|
+
perror("bind"); exit(1);
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
/* Wait to someone connect to this server --------------------- */
|
86
|
+
|
87
|
+
if (listen(*server, MAX_CLIENT) < 0) {
|
88
|
+
|
89
|
+
perror("listen"); exit(2);
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
/* Clear all clients ------------------------------------------ */
|
94
|
+
|
95
|
+
for (i=0; i <= MAX_CLIENT; i++) clients[i]=FREE;
|
96
|
+
|
97
|
+
/* Main loop -------------------------------------------------- */
|
98
|
+
|
99
|
+
for (;;) {
|
100
|
+
|
101
|
+
/* Initalize fd_set ----------------------------------------- */
|
102
|
+
|
103
|
+
FD_ZERO(&fds);
|
104
|
+
|
105
|
+
for (i=0; i < MAX_CLIENT+1; i++) {
|
106
|
+
|
107
|
+
if (sockets[i] != FREE) FD_SET(sockets[i], &fds);
|
108
|
+
|
109
|
+
printf("sokects[%d]=%d\n", i, sockets[i]);
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
/* Check message arrivals ----------------------------------- */
|
114
|
+
|
115
|
+
if ((n = select(FD_SETSIZE, &fds, NULL, NULL, NULL)) == -1) {
|
116
|
+
|
117
|
+
perror("select"); exit(3);
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
/* Processing Loop ------------------------------------------ */
|
122
|
+
|
123
|
+
for (i=0; i < MAX_CLIENT; i++) {
|
124
|
+
|
125
|
+
if (clients[i] != FREE) {
|
126
|
+
|
127
|
+
if (FD_ISSET(clients[i], &fds)) { /* A Message is exist */
|
128
|
+
|
129
|
+
if ((len = read(clients[i], buf, 100))==-1) {
|
130
|
+
|
131
|
+
perror("read"); exit(4);
|
132
|
+
|
133
|
+
} else if (len != 0) {
|
134
|
+
|
135
|
+
if (strncmp(buf, "quit", 4) != 0) {
|
136
|
+
|
137
|
+
printf("A message from a client (%d) is arrived.\n", i);
|
138
|
+
|
139
|
+
printf("client[%d]:%s\n", i, buf);
|
140
|
+
|
141
|
+
for(p = 0; p <= i; p++) {
|
142
|
+
|
143
|
+
write(clients[p], buf, 100);
|
144
|
+
|
145
|
+
bzero(buf, 100);
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
} else {
|
152
|
+
|
153
|
+
printf("A client (%d) leaved.\n", i);
|
154
|
+
|
155
|
+
close(clients[i]); clients[i] = FREE;
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
} else {
|
160
|
+
|
161
|
+
close(clients[i]); clients[i] = FREE;
|
162
|
+
|
163
|
+
printf("A client (%d) leaved.\n", i);
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
/* New connection -------------------------------------------*/
|
174
|
+
|
175
|
+
if (FD_ISSET(*server, &fds) != 0) {
|
176
|
+
|
177
|
+
len = sizeof(caddr);
|
178
|
+
|
179
|
+
for (i=0; i < MAX_CLIENT; i++) {
|
180
|
+
|
181
|
+
if (clients[i] == FREE) break;
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
clients[i] = accept(*server, (struct sockaddr*)&caddr, &len);
|
186
|
+
|
187
|
+
if (clients[i] == -1) {
|
188
|
+
|
189
|
+
perror("accept"); exit(5);
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
if (i < MAX_CLIENT) {
|
194
|
+
|
195
|
+
printf("A new client (%d) is accepted.\n", i);
|
196
|
+
|
197
|
+
} else {
|
198
|
+
|
199
|
+
printf("A client is refused.\n");
|
200
|
+
|
201
|
+
write(clients[i], "Server is too busy.\n", 20);
|
202
|
+
|
203
|
+
close(clients[i]);
|
204
|
+
|
205
|
+
clients[i]=FREE;
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
```
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
クライアントプログラム
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
```c
|
232
|
+
|
233
|
+
#include <stdio.h>
|
234
|
+
|
235
|
+
#include <sys/types.h>
|
236
|
+
|
237
|
+
#include <sys/socket.h>
|
238
|
+
|
239
|
+
#include <netinet/in.h>
|
240
|
+
|
21
241
|
#include <netdb.h>
|
22
242
|
|
23
243
|
|
@@ -108,112 +328,6 @@
|
|
108
328
|
|
109
329
|
|
110
330
|
|
111
|
-
|
112
|
-
|
113
|
-
クライアントプログラム
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
```c
|
118
|
-
|
119
|
-
#include <stdio.h>
|
120
|
-
|
121
|
-
#include <sys/types.h>
|
122
|
-
|
123
|
-
#include <sys/socket.h>
|
124
|
-
|
125
|
-
#include <netinet/in.h>
|
126
|
-
|
127
|
-
#include <netdb.h>
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
#define PORT 19991
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
main(int argc, char *argv[])
|
136
|
-
|
137
|
-
{
|
138
|
-
|
139
|
-
int i, s, t, len;
|
140
|
-
|
141
|
-
struct sockaddr_in addr;
|
142
|
-
|
143
|
-
struct hostent *hp;
|
144
|
-
|
145
|
-
static char buf[100];
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
if (argc != 2) {
|
150
|
-
|
151
|
-
fprintf(stderr, "Usage: net_client serverhost\n");
|
152
|
-
|
153
|
-
exit(1);
|
154
|
-
|
155
|
-
}
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
160
|
-
|
161
|
-
perror("socket");
|
162
|
-
|
163
|
-
exit(1);
|
164
|
-
|
165
|
-
}
|
166
|
-
|
167
|
-
if ((hp = gethostbyname(argv[1])) == NULL) {
|
168
|
-
|
169
|
-
perror("gethostbyname");
|
170
|
-
|
171
|
-
exit(1);
|
172
|
-
|
173
|
-
}
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
bcopy(hp->h_addr, &addr.sin_addr, hp->h_length);
|
178
|
-
|
179
|
-
addr.sin_family = AF_INET;
|
180
|
-
|
181
|
-
addr.sin_port = htons(PORT);
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
if (connect(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) {
|
186
|
-
|
187
|
-
perror("connect");
|
188
|
-
|
189
|
-
exit(1);
|
190
|
-
|
191
|
-
}
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
while (fgets(buf, 100, stdin)) {
|
196
|
-
|
197
|
-
write(s, buf, 100);
|
198
|
-
|
199
|
-
read(s, buf, 100);
|
200
|
-
|
201
|
-
printf("%s", buf);
|
202
|
-
|
203
|
-
bzero(buf, 100);
|
204
|
-
|
205
|
-
}
|
206
|
-
|
207
|
-
close(s);
|
208
|
-
|
209
|
-
}
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
```
|
214
|
-
|
215
|
-
|
216
|
-
|
217
331
|
プログラム はこんな感じです。サーバーの方はコンパイル後「./snet_server 19991」か「./snet_server 127.0.0.0 19991 」どちらかで実行してます。クライアントがうまく実行できません。どのように打てばいいか教えてください。
|
218
332
|
|
219
333
|
|
2
これで整形完了なはずです
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,57 +8,49 @@
|
|
8
8
|
|
9
9
|
・サーバープログラム
|
10
10
|
|
11
|
+
```c
|
12
|
+
|
11
13
|
#include <stdio.h>
|
12
14
|
|
13
15
|
#include <sys/types.h>
|
14
16
|
|
15
17
|
#include <sys/socket.h>
|
16
18
|
|
17
|
-
#include <sys/time.h>
|
18
|
-
|
19
|
-
#include <unistd.h>
|
20
|
-
|
21
19
|
#include <netinet/in.h>
|
22
20
|
|
23
|
-
|
21
|
+
#include <netdb.h>
|
24
|
-
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
-
#define PORT
|
25
|
+
#define PORT 19991
|
26
|
-
|
27
|
-
|
26
|
+
|
28
|
-
|
27
|
+
|
28
|
+
|
29
|
-
|
29
|
+
main(int argc, char *argv[])
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
main()
|
36
30
|
|
37
31
|
{
|
38
32
|
|
39
|
-
int i, j, n, *server, *clients, sockets[MAX_CLIENT+2], len, p;
|
40
|
-
|
41
|
-
|
33
|
+
int i, s, t, len;
|
42
|
-
|
34
|
+
|
43
|
-
struct sockaddr_in
|
35
|
+
struct sockaddr_in addr;
|
44
|
-
|
36
|
+
|
45
|
-
struct s
|
37
|
+
struct hostent *hp;
|
46
|
-
|
38
|
+
|
47
|
-
static char buf[100]
|
39
|
+
static char buf[100];
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
40
|
+
|
54
|
-
|
55
|
-
|
56
|
-
|
41
|
+
|
42
|
+
|
57
|
-
|
43
|
+
if (argc != 2) {
|
44
|
+
|
58
|
-
|
45
|
+
fprintf(stderr, "Usage: net_client serverhost\n");
|
46
|
+
|
59
|
-
|
47
|
+
exit(1);
|
48
|
+
|
60
|
-
|
49
|
+
}
|
50
|
+
|
51
|
+
|
52
|
+
|
61
|
-
if ((
|
53
|
+
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
62
54
|
|
63
55
|
perror("socket");
|
64
56
|
|
@@ -66,153 +58,53 @@
|
|
66
58
|
|
67
59
|
}
|
68
60
|
|
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
|
-
f
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
f
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
/* Check message arrivals ----------------------------------- */
|
112
|
-
|
113
|
-
if ((n = select(FD_SETSIZE, &fds, NULL, NULL, NULL)) == -1) {
|
114
|
-
|
115
|
-
perror("select"); exit(3);
|
116
|
-
|
117
|
-
}
|
118
|
-
|
119
|
-
/* Processing Loop ------------------------------------------ */
|
120
|
-
|
121
|
-
for (i=0; i < MAX_CLIENT; i++) {
|
122
|
-
|
123
|
-
if (clients[i] != FREE) {
|
124
|
-
|
125
|
-
if (FD_ISSET(clients[i], &fds)) { /* A Message is exist */
|
126
|
-
|
127
|
-
if ((len = read(clients[i], buf, 100))==-1) {
|
128
|
-
|
129
|
-
perror("read"); exit(4);
|
130
|
-
|
131
|
-
} else if (len != 0) {
|
132
|
-
|
133
|
-
if (strncmp(buf, "quit", 4) != 0) {
|
134
|
-
|
135
|
-
printf("A message from a client (%d) is arrived.\n", i);
|
136
|
-
|
137
|
-
printf("client[%d]:%s\n", i, buf);
|
138
|
-
|
139
|
-
for(p = 0; p <= i; p++) {
|
140
|
-
|
141
|
-
write(clients[p], buf, 100);
|
142
|
-
|
143
|
-
bzero(buf, 100);
|
144
|
-
|
145
|
-
}
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
} else {
|
150
|
-
|
151
|
-
printf("A client (%d) leaved.\n", i);
|
152
|
-
|
153
|
-
close(clients[i]); clients[i] = FREE;
|
154
|
-
|
155
|
-
}
|
156
|
-
|
157
|
-
} else {
|
158
|
-
|
159
|
-
close(clients[i]); clients[i] = FREE;
|
160
|
-
|
161
|
-
printf("A client (%d) leaved.\n", i);
|
162
|
-
|
163
|
-
}
|
164
|
-
|
165
|
-
}
|
166
|
-
|
167
|
-
}
|
168
|
-
|
169
|
-
}
|
170
|
-
|
171
|
-
/* New connection -------------------------------------------*/
|
172
|
-
|
173
|
-
if (FD_ISSET(*server, &fds) != 0) {
|
174
|
-
|
175
|
-
len = sizeof(caddr);
|
176
|
-
|
177
|
-
for (i=0; i < MAX_CLIENT; i++) {
|
178
|
-
|
179
|
-
if (clients[i] == FREE) break;
|
180
|
-
|
181
|
-
}
|
182
|
-
|
183
|
-
clients[i] = accept(*server, (struct sockaddr*)&caddr, &len);
|
184
|
-
|
185
|
-
if (clients[i] == -1) {
|
186
|
-
|
187
|
-
perror("accept"); exit(5);
|
188
|
-
|
189
|
-
}
|
190
|
-
|
191
|
-
if (i < MAX_CLIENT) {
|
192
|
-
|
193
|
-
printf("A new client (%d) is accepted.\n", i);
|
194
|
-
|
195
|
-
} else {
|
196
|
-
|
197
|
-
printf("A client is refused.\n");
|
198
|
-
|
199
|
-
write(clients[i], "Server is too busy.\n", 20);
|
200
|
-
|
201
|
-
close(clients[i]);
|
202
|
-
|
203
|
-
clients[i]=FREE;
|
204
|
-
|
205
|
-
}
|
206
|
-
|
207
|
-
}
|
208
|
-
|
209
|
-
}
|
61
|
+
if ((hp = gethostbyname(argv[1])) == NULL) {
|
62
|
+
|
63
|
+
perror("gethostbyname");
|
64
|
+
|
65
|
+
exit(1);
|
66
|
+
|
67
|
+
}
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
bcopy(hp->h_addr, &addr.sin_addr, hp->h_length);
|
72
|
+
|
73
|
+
addr.sin_family = AF_INET;
|
74
|
+
|
75
|
+
addr.sin_port = htons(PORT);
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
if (connect(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) {
|
80
|
+
|
81
|
+
perror("connect");
|
82
|
+
|
83
|
+
exit(1);
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
while (fgets(buf, 100, stdin)) {
|
90
|
+
|
91
|
+
write(s, buf, 100);
|
92
|
+
|
93
|
+
read(s, buf, 100);
|
94
|
+
|
95
|
+
printf("%s", buf);
|
96
|
+
|
97
|
+
bzero(buf, 100);
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
close(s);
|
210
102
|
|
211
103
|
}
|
212
104
|
|
213
105
|
|
214
106
|
|
215
|
-
|
107
|
+
```
|
216
108
|
|
217
109
|
|
218
110
|
|
@@ -222,6 +114,8 @@
|
|
222
114
|
|
223
115
|
|
224
116
|
|
117
|
+
```c
|
118
|
+
|
225
119
|
#include <stdio.h>
|
226
120
|
|
227
121
|
#include <sys/types.h>
|
@@ -316,6 +210,8 @@
|
|
316
210
|
|
317
211
|
|
318
212
|
|
213
|
+
```
|
214
|
+
|
319
215
|
|
320
216
|
|
321
217
|
プログラム はこんな感じです。サーバーの方はコンパイル後「./snet_server 19991」か「./snet_server 127.0.0.0 19991 」どちらかで実行してます。クライアントがうまく実行できません。どのように打てばいいか教えてください。
|
1
プロクラムを整形しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,29 +8,27 @@
|
|
8
8
|
|
9
9
|
・サーバープログラム
|
10
10
|
|
11
|
-
include <stdio.h>
|
11
|
+
#include <stdio.h>
|
12
|
-
|
12
|
+
|
13
|
-
include <sys/types.h>
|
13
|
+
#include <sys/types.h>
|
14
|
-
|
14
|
+
|
15
|
-
include <sys/socket.h>
|
15
|
+
#include <sys/socket.h>
|
16
|
-
|
16
|
+
|
17
|
-
include <sys/time.h>
|
17
|
+
#include <sys/time.h>
|
18
|
-
|
18
|
+
|
19
|
-
include <unistd.h>
|
19
|
+
#include <unistd.h>
|
20
|
-
|
20
|
+
|
21
|
-
include <netinet/in.h>
|
21
|
+
#include <netinet/in.h>
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
-
define PORT 19991
|
25
|
+
#define PORT 19991
|
26
|
-
|
26
|
+
|
27
|
-
define MAX_CLIENT 5
|
27
|
+
#define MAX_CLIENT 5
|
28
|
-
|
28
|
+
|
29
|
-
define FREE 100
|
29
|
+
#define FREE 100
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
30
|
+
|
31
|
+
|
34
32
|
|
35
33
|
|
36
34
|
|
@@ -216,23 +214,27 @@
|
|
216
214
|
|
217
215
|
|
218
216
|
|
217
|
+
|
218
|
+
|
219
|
+
|
220
|
+
|
219
221
|
クライアントプログラム
|
220
222
|
|
221
223
|
|
222
224
|
|
223
|
-
include <stdio.h>
|
225
|
+
#include <stdio.h>
|
224
|
-
|
226
|
+
|
225
|
-
include <sys/types.h>
|
227
|
+
#include <sys/types.h>
|
226
|
-
|
228
|
+
|
227
|
-
include <sys/socket.h>
|
229
|
+
#include <sys/socket.h>
|
228
|
-
|
230
|
+
|
229
|
-
include <netinet/in.h>
|
231
|
+
#include <netinet/in.h>
|
230
|
-
|
232
|
+
|
231
|
-
include <netdb.h>
|
233
|
+
#include <netdb.h>
|
232
|
-
|
233
|
-
|
234
|
-
|
234
|
+
|
235
|
+
|
236
|
+
|
235
|
-
define PORT 19991
|
237
|
+
#define PORT 19991
|
236
238
|
|
237
239
|
|
238
240
|
|