質問編集履歴

2

修正

2022/12/16 04:50

投稿

Patao_program
Patao_program

スコア22

test CHANGED
File without changes
test CHANGED
@@ -18,6 +18,7 @@
18
18
  {
19
19
  int socket; // acceptで得たソケット
20
20
  pthread_t thread; // ユーザーのスレッド
21
+   int number; // Client *clientsの配列の添字
21
22
  char *name; // 登録した名前
22
23
  bool is_registered; // 登録したかどうか
23
24
  };
@@ -29,6 +30,333 @@
29
30
  };
30
31
  〜省略
31
32
 
33
+
34
+ /// ソースコード全文
35
+ #include <stdio.h>
36
+ #include <stdlib.h>
37
+ #include <unistd.h>
38
+ #include <sys/socket.h>
39
+ #include <netinet/in.h>
40
+ #include <arpa/inet.h>
41
+ #include <pthread.h>
42
+ #include <stdbool.h>
43
+ #include <string.h>
44
+
45
+ #define USEAGE "Usage: chat_server -p PORT\n"
46
+ #define MAX_CONNECTIONS 100
47
+
48
+ struct Client
49
+ {
50
+ int socket;
51
+ pthread_t thread;
52
+ int number;
53
+ char *name;
54
+ bool is_registered;
55
+ };
56
+
57
+ struct Clients
58
+ {
59
+ struct Client *clients[MAX_CONNECTIONS];
60
+ int count;
61
+ };
62
+
63
+ static int listen_requests(int listen_port);
64
+ static void *recv_message(void *void_client);
65
+ static void analysis_message(char *message, struct Client *client);
66
+ static bool has_name(char *name);
67
+ static int push_client(struct Client *client);
68
+ static void send_message(struct Client *client, char *message);
69
+ static void broadcast_message(struct Client *client, char *message);
70
+ static void *xmalloc(size_t size);
71
+ static void free_client(struct Client *client);
72
+ static void die(char *message);
73
+
74
+ static struct Clients clients_info = {0};
75
+ static pthread_mutex_t mutex;
76
+
77
+ int main(int argc, char *argv[])
78
+ {
79
+ int port, c, socket_fd, c_socket_fd;
80
+
81
+ if (argc < 3)
82
+ {
83
+ fprintf(stderr, USEAGE);
84
+ exit(1);
85
+ }
86
+
87
+ while ((c = getopt(argc, argv, "p:")) != -1)
88
+ {
89
+ switch (c)
90
+ {
91
+ case 'p':
92
+ port = atoi(optarg);
93
+
94
+ if (port < 1024 || port > 65535)
95
+ {
96
+ fprintf(stderr, "ポート番号は、1024から65535の間で指定してください。\n");
97
+ exit(1);
98
+ }
99
+ break;
100
+ case '?':
101
+ fprintf(stderr, USEAGE);
102
+ exit(1);
103
+ }
104
+ }
105
+ pthread_mutex_init(&mutex, NULL);
106
+ socket_fd = listen_requests(port);
107
+
108
+ while (1)
109
+ {
110
+ if ((c_socket_fd = accept(socket_fd, NULL, NULL)) == -1)
111
+ die("accept(2)");
112
+
113
+ struct Client *client = NULL;
114
+ client = xmalloc(sizeof(struct Client));
115
+ memset(client, 0, sizeof(struct Client));
116
+
117
+ client->socket = c_socket_fd;
118
+ client->name = NULL;
119
+ client->is_registered = false;
120
+ client->number = push_client(client);
121
+
122
+ pthread_create(&client->thread, NULL, recv_message, client);
123
+ pthread_detach(client->thread);
124
+ }
125
+
126
+ pthread_mutex_destroy(&mutex);
127
+
128
+ return 0;
129
+ }
130
+
131
+ static void *recv_message(void *void_client)
132
+ {
133
+ struct Client *client = void_client;
134
+ int recv_size;
135
+ char recv_buf[1024];
136
+ char *send_text;
137
+
138
+ printf("%d\n", client->number);
139
+
140
+ while (1)
141
+ {
142
+ recv_size = recv(client->socket, recv_buf, sizeof recv_buf, 0);
143
+ if (recv_size == -1)
144
+ die("recv");
145
+ else if (recv_size == 0)
146
+ {
147
+
148
+ send_text = xmalloc(strlen(client->name) + 100);
149
+ sprintf(send_text, "MESSAGE %sさんが、チャットから退出しました。\n", client->name);
150
+ broadcast_message(client, send_text);
151
+ free(send_text);
152
+ free_client(client);
153
+ return NULL;
154
+ }
155
+ else
156
+ analysis_message(recv_buf, client);
157
+ }
158
+
159
+ return NULL;
160
+ }
161
+
162
+ static void analysis_message(char *message, struct Client *client)
163
+ {
164
+ char *method;
165
+ char *send_text;
166
+ char *m;
167
+ char *tmp;
168
+
169
+ tmp = xmalloc(strlen(message) + 1);
170
+ strcpy(tmp, message);
171
+ printf("%s\n", message);
172
+
173
+ /*
174
+ chat プロトコル
175
+
176
+ ユーザー登録
177
+ "CONNECT username",
178
+ "CONNECT statuscode status_message"
179
+ メッセージ送信・受信
180
+ "MESSAGE (BODY)"
181
+
182
+ */
183
+
184
+ m = strchr(message, ' ');
185
+ *m++ = '\0';
186
+ method = xmalloc(m - message);
187
+
188
+ strcpy(method, message);
189
+ // message = m;
190
+
191
+ if (!strcmp("CONNECT", method) && !client->is_registered)
192
+ {
193
+ if (!has_name(m))
194
+ {
195
+ pthread_mutex_lock(&mutex);
196
+ client->name = xmalloc(strlen(m) + 1);
197
+ strcpy(client->name, m);
198
+ pthread_mutex_unlock(&mutex);
199
+ client->is_registered = true;
200
+ send_text = xmalloc(strlen("CONNECT 200 OK") + 1);
201
+ strcpy(send_text, "CONNECT 200 OK");
202
+ send_message(client, send_text);
203
+ send_text = realloc(send_text, strlen(client->name) + 100);
204
+ if (send_text == NULL)
205
+ die("realloc(3)");
206
+ sprintf(send_text, "MESSAGE %sさんが、チャットに参加しました。\n", client->name);
207
+ broadcast_message(client, send_text);
208
+ free(send_text);
209
+ }
210
+ else
211
+ {
212
+ send_text = xmalloc(strlen("CONNECT 400 The name is exists") + 1);
213
+ strcpy(send_text, "CONNECT 400 The name is exists");
214
+ send_message(client, send_text);
215
+ free(send_text);
216
+ }
217
+ }
218
+ else if (!strcmp("MESSAGE", method) && client->is_registered)
219
+ {
220
+ send_text = xmalloc(strlen(m) + 1);
221
+ strcpy(send_text, tmp);
222
+ broadcast_message(client, send_text);
223
+ free(send_text);
224
+ }
225
+ // else
226
+ // {
227
+ // }
228
+
229
+ printf("%s\n", method);
230
+ free(tmp);
231
+ free(method);
232
+ }
233
+
234
+ static int listen_requests(int listen_port)
235
+ {
236
+ int socket_fd;
237
+ struct sockaddr_in addr;
238
+
239
+ socket_fd = socket(AF_INET, SOCK_STREAM, 0);
240
+
241
+ if (socket_fd < 0)
242
+ die("socket(2)");
243
+
244
+ addr.sin_family = AF_INET; // インターネットドメイン(IPv4)
245
+ addr.sin_addr.s_addr = INADDR_ANY; // 全てのアドレスからの接続を受け入れる(=0.0.0.0)
246
+ addr.sin_port = htons(listen_port);
247
+
248
+ if (bind(socket_fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) == -1)
249
+ die("bind(2)");
250
+
251
+ if (listen(socket_fd, MAX_CONNECTIONS) == -1)
252
+ die("listen(2)");
253
+
254
+ return socket_fd;
255
+ }
256
+
257
+ static int push_client(struct Client *client)
258
+ {
259
+ int client_num, i;
260
+
261
+ pthread_mutex_lock(&mutex);
262
+
263
+ for (i = 0; i < MAX_CONNECTIONS; i++)
264
+ {
265
+ if (clients_info.clients[i] == NULL)
266
+ {
267
+ clients_info.clients[i] = client;
268
+ client_num = i;
269
+ break;
270
+ }
271
+ }
272
+
273
+ clients_info.count++;
274
+
275
+ pthread_mutex_unlock(&mutex);
276
+
277
+ return client_num;
278
+ }
279
+
280
+ static bool has_name(char *name)
281
+ {
282
+ int i;
283
+ pthread_mutex_lock(&mutex);
284
+
285
+ for (i = 0; i < MAX_CONNECTIONS; i++)
286
+ {
287
+ if (clients_info.clients[i] != NULL && clients_info.clients[i]->name != NULL)
288
+ {
289
+ if (!strcmp(clients_info.clients[i]->name, name))
290
+ return true;
291
+ }
292
+ }
293
+
294
+ pthread_mutex_unlock(&mutex);
295
+
296
+ return false;
297
+ }
298
+
299
+ static void send_message(struct Client *client, char *message)
300
+ {
301
+ ssize_t size;
302
+
303
+ pthread_mutex_lock(&mutex);
304
+
305
+ size = send(client->socket, message, strlen(message) + 1, 0);
306
+
307
+ pthread_mutex_unlock(&mutex);
308
+ if (size < 0)
309
+ die("send(2)");
310
+ }
311
+
312
+ static void broadcast_message(struct Client *client, char *message)
313
+ {
314
+ int i;
315
+
316
+ pthread_mutex_lock(&mutex);
317
+
318
+ for (i = 0; i < MAX_CONNECTIONS; i++)
319
+ {
320
+ if (clients_info.clients[i] != NULL)
321
+ {
322
+ ssize_t size;
323
+
324
+ size = send(clients_info.clients[i]->socket, message, strlen(message) + 1, 0);
325
+ if (size < 0)
326
+ die("send(2)");
327
+ }
328
+ }
329
+
330
+ pthread_mutex_unlock(&mutex);
331
+ }
332
+
333
+ static void free_client(struct Client *client)
334
+ {
335
+ pthread_mutex_lock(&mutex);
336
+ clients_info.clients[client->number] = NULL;
337
+ clients_info.count--;
338
+ free(client->name);
339
+ free(client);
340
+ pthread_mutex_unlock(&mutex);
341
+ }
342
+
343
+ static void *xmalloc(size_t size)
344
+ {
345
+ void *p;
346
+ p = malloc(size);
347
+
348
+ if (!p)
349
+ die("malloc(3)");
350
+
351
+ return p;
352
+ }
353
+
354
+ static void die(char *message)
355
+ {
356
+ perror(message);
357
+ exit(1);
358
+ }
359
+
32
360
  ```
33
361
 
34
362
  ### 考えたこと

1

変更

2022/12/14 03:50

投稿

Patao_program
Patao_program

スコア22

test CHANGED
File without changes
test CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  ### 実現したいこと
7
7
  クライアントが接続してきたら、配列にユーザー情報を格納、切断した場合ユーザー情報削除をしたいです。
8
- どのような実装が最適でしょうか?
8
+ どのような実装が最適でしょうか?また、下記のような実装でも問題はないでしょうか?
9
9
 
10
10
  ### 該当のソースコード
11
11