-
Notifications
You must be signed in to change notification settings - Fork 0
Create server.c #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| #include <string.h> | ||
| #include <sys/socket.h> | ||
| #include <unistd.h> | ||
| #define PORT 8080 |
There was a problem hiding this comment.
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[]) |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
accept の第3引数はint * をうけるので、書き込み可能な変数のアドレスを渡す。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
accept をループで回すようにしたい。(複数のクライアントを処理する)
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
read は一発で読めないことがあるので、それの対応をする。
No description provided.