Skip to content

HTTPサーバーを作ってみる#20

Open
hiroki-horiguchi-dev wants to merge 4 commits into
mainfrom
socket-programming
Open

HTTPサーバーを作ってみる#20
hiroki-horiguchi-dev wants to merge 4 commits into
mainfrom
socket-programming

Conversation

@hiroki-horiguchi-dev
Copy link
Copy Markdown
Owner

@hiroki-horiguchi-dev hiroki-horiguchi-dev commented May 24, 2026

CSZAP 4th 課題

@hiroki-horiguchi-dev hiroki-horiguchi-dev self-assigned this May 24, 2026
Comment thread socket_programming/client.c Outdated
int sfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sfd < 0) { perror("socket"); return 1; }

if (connect(sfd, res->ai_addr, res->ai_addrlen) < 0) { perror("connect"); return 1; }
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

失敗した場合に sfd を close していない点が気になりました。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

取り込みます

Comment thread socket_programming/client.c Outdated
if (rv != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return 1; }

int sfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sfd < 0) { perror("socket"); return 1; }
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

失敗した場合に res を freeaddrinfo() していない点が気になりました。関数の末尾に、リソースが確保されている場合は解放するコードを書き、途中で失敗した場合は、そこまで goto で飛ばす、という書き方を時々見かけます。賛否はあるものの、参考まで。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

メリット: 解放処理がまとまる
デメリット: goto が使われすぎると、スパゲッティコードになる可能性がある
こんなところでしょうか

Comment thread socket_programming/server.c Outdated
ssize_t total = 0;
while (total < BUF_SIZE - 1) {
ssize_t n = read(fd, req + total, BUF_SIZE - 1 - total);
if (n < 0) { perror("read"); free(req); return; }
Copy link
Copy Markdown

@nodchip nodchip May 26, 2026

Choose a reason for hiding this comment

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

個人的には、生でメモリを確保し、解放する際は、解放したあとにポインターに NULL を代入します。理由は、悪意ある者が、メモリのダンプを覗いたとき、ポインターの値から、解放済みのメモリ領域を覗き、価値のあるデータを盗むのを防ぎたいためです。趣味の範囲かもしれません。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。なるほど、取り込みます。

@hiroki-horiguchi-dev
Copy link
Copy Markdown
Owner Author

@nodchip
コメントいただきありがとうございます、今晩に確認いたします!

@hiroki-horiguchi-dev
Copy link
Copy Markdown
Owner Author

hiroki-horiguchi-dev commented May 28, 2026

メモ
BACKLOGで10が指定されるため、挙動としては11以上のリクエストはQueueに載らないと読んでいたが、Macの場合は実際にはkern.ipc.somaxconn: 128で書き換えられるよう。129を超えるとアクセス拒否になるということらしい。

・曖昧な理解だったDNSの話
・クライアント側の処理の流れ
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.

2 participants