Skip to content

Conversation

@nittoco
Copy link
Owner

@nittoco nittoco commented Oct 11, 2024

No description provided.

@nittoco
Copy link
Owner Author

nittoco commented Oct 11, 2024

server.cは、以下のレビューに対応

  • #include が全部終わった後には空行をおきたい。
  • 関数が始まる前にも空行をおきたい。
  • sockopt とか socket_option とかの名前にしたい。
  • コマンドライン引数でアドレス・ポートを指定できるようにしたい。
  • accept の第3引数はint * をうけるので、書き込み可能な変数のアドレスを渡す。
  • accept をループで回すようにしたい。(複数のクライアントを処理する)
  • 2,3 引数は client_address に関連する sockaddr
  • read は一発で読めないことがあるので、それの対応をする。

加えて、マルチスレッドにも挑戦

fprintf(stderr, "Failed to bind to any address\n");
goto cleanup;
}
freeaddrinfo(res);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

freeaddrinfo した後に res=NULL してあげて、2回目の解放を防ぐ。

char buffer[BUFFER_SIZE];
ssize_t bytes_read;
while ((bytes_read = read(client_socket, buffer, sizeof(buffer))) > 0) {
if (write(STDOUT_FILENO, buffer, bytes_read) < 0) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

標準出力に対して libc (printf など) と syscall を混ぜて使うと、
libc への printf がバッファされた結果、後からの write が先に出力されるなどの現象が起きるので、
混ぜて使わない。 (もしくはそのバッファリングまで意識したコードを書く (地獄))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants