質問編集履歴
4
server側はおそらく動いています。
title
CHANGED
File without changes
|
body
CHANGED
@@ -166,4 +166,9 @@
|
|
166
166
|
プログラム はこんな感じです。サーバーの方はコンパイル後「./snet_server 19991」か「./snet_server 127.0.0.0 19991 」どちらかで実行してます。クライアントがうまく実行できません。どのように打てばいいか教えてください。
|
167
167
|
|
168
168
|
|
169
|
-
初めて書くので見にくかったり書き方が下手だとは思うんですけどよろしくお願いします。
|
169
|
+
初めて書くので見にくかったり書き方が下手だとは思うんですけどよろしくお願いします。
|
170
|
+
|
171
|
+
netstat -antのコマンドを実行するとserverプログラムが”LISTEN”状態になっているのでこちらは問題なく動作していると思います。ただAddressが見えないのでそのアドレスをクライアント側の引数に入れると動作すると思っています。
|
172
|
+
|
173
|
+
Proto Recv-Q Send-Q Local Address Foreign Address (state)
|
174
|
+
tcp4 0 0 *.19991 *.* LISTEN
|
3
すいません。おんなじになってました
title
CHANGED
File without changes
|
body
CHANGED
@@ -7,50 +7,107 @@
|
|
7
7
|
#include <stdio.h>
|
8
8
|
#include <sys/types.h>
|
9
9
|
#include <sys/socket.h>
|
10
|
+
#include <sys/time.h>
|
11
|
+
#include <unistd.h>
|
10
12
|
#include <netinet/in.h>
|
11
|
-
#include <netdb.h>
|
12
13
|
|
13
|
-
#define PORT
|
14
|
+
#define PORT 19991
|
15
|
+
#define MAX_CLIENT 5
|
16
|
+
#define FREE 100
|
14
17
|
|
18
|
+
|
15
|
-
main(
|
19
|
+
main()
|
16
20
|
{
|
21
|
+
int i, j, n, *server, *clients, sockets[MAX_CLIENT+2], len, p;
|
17
|
-
|
22
|
+
fd_set fds;
|
18
|
-
struct sockaddr_in
|
23
|
+
struct sockaddr_in saddr;
|
19
|
-
struct
|
24
|
+
struct sockaddr_in caddr;
|
20
|
-
static char buf[100];
|
25
|
+
static char buf[100], x[100];
|
26
|
+
|
27
|
+
|
21
|
-
|
28
|
+
/* Intalize a socket ------------------------------------------ */
|
29
|
+
|
22
|
-
|
30
|
+
server = sockets;
|
23
|
-
fprintf(stderr, "Usage: net_client serverhost\n");
|
24
|
-
|
31
|
+
clients = sockets+1;
|
25
|
-
}
|
26
|
-
|
27
|
-
if ((
|
32
|
+
if ((*server = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
28
33
|
perror("socket");
|
29
34
|
exit(1);
|
30
35
|
}
|
36
|
+
/* Bind the socket -------------------------------------------- */
|
37
|
+
saddr.sin_family = AF_INET;
|
31
|
-
|
38
|
+
saddr.sin_addr.s_addr = INADDR_ANY;
|
39
|
+
saddr.sin_port = htons(PORT);
|
40
|
+
if (bind(*server,(struct sockaddr *)&saddr,sizeof(struct sockaddr_in))<0) {
|
32
|
-
|
41
|
+
perror("bind"); exit(1);
|
33
|
-
exit(1);
|
34
42
|
}
|
35
|
-
|
36
|
-
bcopy(hp->h_addr, &addr.sin_addr, hp->h_length);
|
37
|
-
addr.sin_family = AF_INET;
|
38
|
-
addr.sin_port = htons(PORT);
|
39
|
-
|
40
|
-
|
43
|
+
/* Wait to someone connect to this server --------------------- */
|
44
|
+
if (listen(*server, MAX_CLIENT) < 0) {
|
41
|
-
perror("
|
45
|
+
perror("listen"); exit(2);
|
42
|
-
exit(1);
|
43
46
|
}
|
44
|
-
|
47
|
+
/* Clear all clients ------------------------------------------ */
|
48
|
+
for (i=0; i <= MAX_CLIENT; i++) clients[i]=FREE;
|
49
|
+
/* Main loop -------------------------------------------------- */
|
50
|
+
for (;;) {
|
51
|
+
/* Initalize fd_set ----------------------------------------- */
|
52
|
+
FD_ZERO(&fds);
|
53
|
+
for (i=0; i < MAX_CLIENT+1; i++) {
|
54
|
+
if (sockets[i] != FREE) FD_SET(sockets[i], &fds);
|
55
|
+
printf("sokects[%d]=%d\n", i, sockets[i]);
|
56
|
+
}
|
57
|
+
/* Check message arrivals ----------------------------------- */
|
58
|
+
if ((n = select(FD_SETSIZE, &fds, NULL, NULL, NULL)) == -1) {
|
59
|
+
perror("select"); exit(3);
|
60
|
+
}
|
61
|
+
/* Processing Loop ------------------------------------------ */
|
62
|
+
for (i=0; i < MAX_CLIENT; i++) {
|
63
|
+
if (clients[i] != FREE) {
|
64
|
+
if (FD_ISSET(clients[i], &fds)) { /* A Message is exist */
|
45
|
-
|
65
|
+
if ((len = read(clients[i], buf, 100))==-1) {
|
66
|
+
perror("read"); exit(4);
|
67
|
+
} else if (len != 0) {
|
68
|
+
if (strncmp(buf, "quit", 4) != 0) {
|
69
|
+
printf("A message from a client (%d) is arrived.\n", i);
|
70
|
+
printf("client[%d]:%s\n", i, buf);
|
71
|
+
for(p = 0; p <= i; p++) {
|
46
|
-
|
72
|
+
write(clients[p], buf, 100);
|
47
|
-
read(s, buf, 100);
|
48
|
-
printf("%s", buf);
|
49
|
-
|
73
|
+
bzero(buf, 100);
|
74
|
+
}
|
75
|
+
|
76
|
+
} else {
|
77
|
+
printf("A client (%d) leaved.\n", i);
|
78
|
+
close(clients[i]); clients[i] = FREE;
|
79
|
+
}
|
80
|
+
} else {
|
81
|
+
close(clients[i]); clients[i] = FREE;
|
82
|
+
printf("A client (%d) leaved.\n", i);
|
83
|
+
}
|
84
|
+
}
|
85
|
+
}
|
86
|
+
}
|
87
|
+
/* New connection -------------------------------------------*/
|
88
|
+
if (FD_ISSET(*server, &fds) != 0) {
|
89
|
+
len = sizeof(caddr);
|
90
|
+
for (i=0; i < MAX_CLIENT; i++) {
|
91
|
+
if (clients[i] == FREE) break;
|
92
|
+
}
|
93
|
+
clients[i] = accept(*server, (struct sockaddr*)&caddr, &len);
|
94
|
+
if (clients[i] == -1) {
|
95
|
+
perror("accept"); exit(5);
|
96
|
+
}
|
97
|
+
if (i < MAX_CLIENT) {
|
98
|
+
printf("A new client (%d) is accepted.\n", i);
|
99
|
+
} else {
|
100
|
+
printf("A client is refused.\n");
|
101
|
+
write(clients[i], "Server is too busy.\n", 20);
|
102
|
+
close(clients[i]);
|
103
|
+
clients[i]=FREE;
|
104
|
+
}
|
105
|
+
}
|
50
106
|
}
|
51
|
-
close(s);
|
52
107
|
}
|
53
108
|
|
109
|
+
|
110
|
+
|
54
111
|
```
|
55
112
|
|
56
113
|
|
2
これで整形完了なはずです
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,113 +3,60 @@
|
|
3
3
|
ターミナルで何をどのように入力したら良いか教えてください。ソースコードも一応できでいるのですが、おそらく結構な部分間違っているのでご了承ください。一応乗せておきます
|
4
4
|
|
5
5
|
・サーバープログラム
|
6
|
+
```c
|
6
7
|
#include <stdio.h>
|
7
8
|
#include <sys/types.h>
|
8
9
|
#include <sys/socket.h>
|
9
|
-
#include <sys/time.h>
|
10
|
-
#include <unistd.h>
|
11
10
|
#include <netinet/in.h>
|
11
|
+
#include <netdb.h>
|
12
12
|
|
13
|
-
#define PORT
|
13
|
+
#define PORT 19991
|
14
|
-
#define MAX_CLIENT 5
|
15
|
-
#define FREE 100
|
16
14
|
|
15
|
+
main(int argc, char *argv[])
|
16
|
+
{
|
17
|
+
int i, s, t, len;
|
18
|
+
struct sockaddr_in addr;
|
19
|
+
struct hostent *hp;
|
20
|
+
static char buf[100];
|
17
21
|
|
22
|
+
if (argc != 2) {
|
23
|
+
fprintf(stderr, "Usage: net_client serverhost\n");
|
18
|
-
|
24
|
+
exit(1);
|
19
|
-
|
25
|
+
}
|
20
|
-
|
26
|
+
|
21
|
-
fd_set fds;
|
22
|
-
struct sockaddr_in saddr;
|
23
|
-
struct sockaddr_in caddr;
|
24
|
-
static char buf[100], x[100];
|
25
|
-
|
26
|
-
|
27
|
-
/* Intalize a socket ------------------------------------------ */
|
28
|
-
|
29
|
-
server = sockets;
|
30
|
-
clients = sockets+1;
|
31
|
-
if ((
|
27
|
+
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
32
28
|
perror("socket");
|
33
29
|
exit(1);
|
34
30
|
}
|
35
|
-
/* Bind the socket -------------------------------------------- */
|
36
|
-
saddr.sin_family = AF_INET;
|
37
|
-
|
31
|
+
if ((hp = gethostbyname(argv[1])) == NULL) {
|
38
|
-
saddr.sin_port = htons(PORT);
|
39
|
-
if (bind(*server,(struct sockaddr *)&saddr,sizeof(struct sockaddr_in))<0) {
|
40
|
-
|
32
|
+
perror("gethostbyname");
|
33
|
+
exit(1);
|
41
34
|
}
|
35
|
+
|
36
|
+
bcopy(hp->h_addr, &addr.sin_addr, hp->h_length);
|
37
|
+
addr.sin_family = AF_INET;
|
38
|
+
addr.sin_port = htons(PORT);
|
39
|
+
|
42
|
-
|
40
|
+
if (connect(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) {
|
43
|
-
if (listen(*server, MAX_CLIENT) < 0) {
|
44
|
-
perror("
|
41
|
+
perror("connect");
|
42
|
+
exit(1);
|
45
43
|
}
|
46
|
-
|
44
|
+
|
47
|
-
for (i=0; i <= MAX_CLIENT; i++) clients[i]=FREE;
|
48
|
-
/* Main loop -------------------------------------------------- */
|
49
|
-
for (;;) {
|
50
|
-
/* Initalize fd_set ----------------------------------------- */
|
51
|
-
FD_ZERO(&fds);
|
52
|
-
for (i=0; i < MAX_CLIENT+1; i++) {
|
53
|
-
if (sockets[i] != FREE) FD_SET(sockets[i], &fds);
|
54
|
-
printf("sokects[%d]=%d\n", i, sockets[i]);
|
55
|
-
}
|
56
|
-
/* Check message arrivals ----------------------------------- */
|
57
|
-
if ((n = select(FD_SETSIZE, &fds, NULL, NULL, NULL)) == -1) {
|
58
|
-
perror("select"); exit(3);
|
59
|
-
}
|
60
|
-
/* Processing Loop ------------------------------------------ */
|
61
|
-
for (i=0; i < MAX_CLIENT; i++) {
|
62
|
-
if (clients[i] != FREE) {
|
63
|
-
if (FD_ISSET(clients[i], &fds)) { /* A Message is exist */
|
64
|
-
|
45
|
+
while (fgets(buf, 100, stdin)) {
|
65
|
-
perror("read"); exit(4);
|
66
|
-
} else if (len != 0) {
|
67
|
-
if (strncmp(buf, "quit", 4) != 0) {
|
68
|
-
printf("A message from a client (%d) is arrived.\n", i);
|
69
|
-
printf("client[%d]:%s\n", i, buf);
|
70
|
-
for(p = 0; p <= i; p++) {
|
71
|
-
|
46
|
+
write(s, buf, 100);
|
47
|
+
read(s, buf, 100);
|
48
|
+
printf("%s", buf);
|
72
|
-
|
49
|
+
bzero(buf, 100);
|
73
|
-
}
|
74
|
-
|
75
|
-
} else {
|
76
|
-
printf("A client (%d) leaved.\n", i);
|
77
|
-
close(clients[i]); clients[i] = FREE;
|
78
|
-
}
|
79
|
-
} else {
|
80
|
-
close(clients[i]); clients[i] = FREE;
|
81
|
-
printf("A client (%d) leaved.\n", i);
|
82
|
-
}
|
83
|
-
}
|
84
|
-
}
|
85
|
-
}
|
86
|
-
/* New connection -------------------------------------------*/
|
87
|
-
if (FD_ISSET(*server, &fds) != 0) {
|
88
|
-
len = sizeof(caddr);
|
89
|
-
for (i=0; i < MAX_CLIENT; i++) {
|
90
|
-
if (clients[i] == FREE) break;
|
91
|
-
}
|
92
|
-
clients[i] = accept(*server, (struct sockaddr*)&caddr, &len);
|
93
|
-
if (clients[i] == -1) {
|
94
|
-
perror("accept"); exit(5);
|
95
|
-
}
|
96
|
-
if (i < MAX_CLIENT) {
|
97
|
-
printf("A new client (%d) is accepted.\n", i);
|
98
|
-
} else {
|
99
|
-
printf("A client is refused.\n");
|
100
|
-
write(clients[i], "Server is too busy.\n", 20);
|
101
|
-
close(clients[i]);
|
102
|
-
clients[i]=FREE;
|
103
|
-
}
|
104
|
-
}
|
105
50
|
}
|
51
|
+
close(s);
|
106
52
|
}
|
107
53
|
|
54
|
+
```
|
108
55
|
|
109
56
|
|
110
|
-
|
111
57
|
クライアントプログラム
|
112
58
|
|
59
|
+
```c
|
113
60
|
#include <stdio.h>
|
114
61
|
#include <sys/types.h>
|
115
62
|
#include <sys/socket.h>
|
@@ -157,6 +104,7 @@
|
|
157
104
|
close(s);
|
158
105
|
}
|
159
106
|
|
107
|
+
```
|
160
108
|
|
161
109
|
プログラム はこんな感じです。サーバーの方はコンパイル後「./snet_server 19991」か「./snet_server 127.0.0.0 19991 」どちらかで実行してます。クライアントがうまく実行できません。どのように打てばいいか教えてください。
|
162
110
|
|
1
プロクラムを整形しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,18 +3,17 @@
|
|
3
3
|
ターミナルで何をどのように入力したら良いか教えてください。ソースコードも一応できでいるのですが、おそらく結構な部分間違っているのでご了承ください。一応乗せておきます
|
4
4
|
|
5
5
|
・サーバープログラム
|
6
|
-
include <stdio.h>
|
6
|
+
#include <stdio.h>
|
7
|
-
include <sys/types.h>
|
7
|
+
#include <sys/types.h>
|
8
|
-
include <sys/socket.h>
|
8
|
+
#include <sys/socket.h>
|
9
|
-
include <sys/time.h>
|
9
|
+
#include <sys/time.h>
|
10
|
-
include <unistd.h>
|
10
|
+
#include <unistd.h>
|
11
|
-
include <netinet/in.h>
|
11
|
+
#include <netinet/in.h>
|
12
12
|
|
13
|
-
define PORT 19991
|
13
|
+
#define PORT 19991
|
14
|
-
define MAX_CLIENT 5
|
14
|
+
#define MAX_CLIENT 5
|
15
|
-
define FREE 100
|
15
|
+
#define FREE 100
|
16
16
|
|
17
|
-
**/*↑#があると見にくくなってたので消しました */**
|
18
17
|
|
19
18
|
main()
|
20
19
|
{
|
@@ -107,15 +106,17 @@
|
|
107
106
|
}
|
108
107
|
|
109
108
|
|
109
|
+
|
110
|
+
|
110
111
|
クライアントプログラム
|
111
112
|
|
112
|
-
include <stdio.h>
|
113
|
+
#include <stdio.h>
|
113
|
-
include <sys/types.h>
|
114
|
+
#include <sys/types.h>
|
114
|
-
include <sys/socket.h>
|
115
|
+
#include <sys/socket.h>
|
115
|
-
include <netinet/in.h>
|
116
|
+
#include <netinet/in.h>
|
116
|
-
include <netdb.h>
|
117
|
+
#include <netdb.h>
|
117
118
|
|
118
|
-
define PORT 19991
|
119
|
+
#define PORT 19991
|
119
120
|
|
120
121
|
main(int argc, char *argv[])
|
121
122
|
{
|