Skip to content

Conversation

@nittoco
Copy link
Owner

@nittoco nittoco commented Oct 4, 2024

No description provided.

#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8080
Copy link

Choose a reason for hiding this comment

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

#include が全部終わった後には空行をおきたい。

#include <unistd.h>
#define PORT 8080
#define BUFFER_SIZE 1024
int main(int argc, char const* argv[])
Copy link

Choose a reason for hiding this comment

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

関数が始まる前にも空行をおきたい。

int server_socket, new_socket;
char* message = "I'm server";
struct sockaddr_in server_address;
int option = 1;
Copy link

Choose a reason for hiding this comment

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

sockopt とか socket_option とかの名前にしたい。

exit(1);
}
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = INADDR_ANY;
Copy link

Choose a reason for hiding this comment

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

コマンドライン引数でアドレス・ポートを指定できるようにしたい。

exit(1);
}

new_socket = accept(server_socket, (struct sockadd*)&server_address, sizeof(server_address));
Copy link

Choose a reason for hiding this comment

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

accept の第3引数はint * をうけるので、書き込み可能な変数のアドレスを渡す。

Copy link

Choose a reason for hiding this comment

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

accept をループで回すようにしたい。(複数のクライアントを処理する)

Copy link

Choose a reason for hiding this comment

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

2,3 引数は client_address に関連する sockaddr


new_socket = accept(server_socket, (struct sockadd*)&server_address, sizeof(server_address));
if(new_socket < 0){
perror("Can't bind");
Copy link

Choose a reason for hiding this comment

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

bind じゃない

}


read(new_socket, buffer, BUFFER_SIZE-1)
Copy link

Choose a reason for hiding this comment

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

read は一発で読めないことがあるので、それの対応をする。

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