-
Notifications
You must be signed in to change notification settings - Fork 0
修正版 #2
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
Open
nittoco
wants to merge
2
commits into
main
Choose a base branch
from
修正版
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
The head ref may contain hidden characters: "\u4FEE\u6B63\u7248"
Open
修正版 #2
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| #include <arpa/inet.h> | ||
| #include <netdb.h> | ||
| #include <netinet/in.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <sys/socket.h> | ||
| #include <unistd.h> | ||
|
|
||
| #define BUFFER_SIZE 1024 | ||
|
|
||
| int main(int argc, char *argv[]) { | ||
| int client_socket = -1; | ||
| struct addrinfo hints, *res = NULL; | ||
| char *address = NULL; | ||
| char *port = NULL; | ||
| if (argc < 3) { | ||
| fprintf(stderr, "Usage: %s <server_address> <server_port>\n", argv[0]); | ||
| exit(1); | ||
| } | ||
| address = argv[1]; | ||
| port = argv[2]; | ||
| memset(&hints, 0, sizeof(hints)); | ||
| hints.ai_family = AF_UNSPEC; | ||
| hints.ai_socktype = SOCK_STREAM; | ||
| int status = getaddrinfo(address, port, &hints, &res); | ||
| if (status != 0) { | ||
| fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status)); | ||
| exit(1); | ||
| } | ||
| struct addrinfo *p; | ||
| for (p = res; p != NULL; p = p->ai_next) { | ||
| client_socket = socket(p->ai_family, p->ai_socktype, p->ai_protocol); | ||
| if (client_socket < 0) { | ||
| perror("Socket Error"); | ||
| continue; | ||
| } | ||
| if (connect(client_socket, p->ai_addr, p->ai_addrlen) < 0) { | ||
| perror("Connect Error"); | ||
| close(client_socket); | ||
| continue; | ||
| } | ||
| break; | ||
| } | ||
| if (p == NULL) { | ||
| fprintf(stderr, "Failed to connect to %s:%s\n", address, port); | ||
| freeaddrinfo(res); | ||
| exit(1); | ||
| } | ||
| freeaddrinfo(res); | ||
| printf("Connected to %s:%s\n", address, port); | ||
| char send_buffer[BUFFER_SIZE]; | ||
| char recv_buffer[BUFFER_SIZE]; | ||
| ssize_t bytes_sent, bytes_received; | ||
| while (fgets(send_buffer, sizeof(send_buffer), stdin) != NULL) { | ||
| size_t len = strlen(send_buffer); | ||
| bytes_sent = send(client_socket, send_buffer, len, 0); | ||
| if (bytes_sent < 0) { | ||
| perror("Send Error"); | ||
| break; | ||
| } | ||
| } | ||
| printf("Closing connection.\n"); | ||
| close(client_socket); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| #include <arpa/inet.h> | ||
| #include <netdb.h> | ||
| #include <netinet/in.h> | ||
| #include <pthread.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <sys/socket.h> | ||
| #include <unistd.h> | ||
|
|
||
| #define BUFFER_SIZE 1024 | ||
|
|
||
| void *handle_client(void *arg); | ||
|
|
||
| int main(int argc, char *argv[]) { | ||
| int server_socket = -1; | ||
| struct addrinfo hints, *res = NULL; | ||
| int socket_option = 1; | ||
| char *address = NULL; | ||
| char *port = NULL; | ||
| if (argc >= 2) { | ||
| address = argv[1]; | ||
| } else { | ||
| address = "0.0.0.0"; | ||
| } | ||
| if (argc >= 3) { | ||
| port = argv[2]; | ||
| } else { | ||
| port = "8080"; | ||
| } | ||
| memset(&hints, 0, sizeof(hints)); | ||
| hints.ai_family = AF_UNSPEC; | ||
| hints.ai_socktype = SOCK_STREAM; | ||
| hints.ai_flags = AI_PASSIVE; | ||
| int status = getaddrinfo(address, port, &hints, &res); | ||
| if (status != 0) { | ||
| fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status)); | ||
| goto cleanup; | ||
| } | ||
| struct addrinfo *p; | ||
| for (p = res; p != NULL; p = p->ai_next) { | ||
| server_socket = socket(p->ai_family, p->ai_socktype, p->ai_protocol); | ||
| if (server_socket < 0) { | ||
| perror("Socket Error"); | ||
| continue; | ||
| } | ||
| if (setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &socket_option, sizeof(socket_option)) < 0) { | ||
| perror("Can't set socket option"); | ||
| close(server_socket); | ||
| continue; | ||
| } | ||
| if (bind(server_socket, p->ai_addr, p->ai_addrlen) < 0) { | ||
| perror("Can't bind"); | ||
| close(server_socket); | ||
| continue; | ||
| } | ||
| break; | ||
| } | ||
| if (p == NULL) { | ||
| fprintf(stderr, "Failed to bind to any address\n"); | ||
| goto cleanup; | ||
| } | ||
| freeaddrinfo(res); | ||
| if (listen(server_socket, SOMAXCONN) < 0) { | ||
| perror("Can't listen"); | ||
| goto cleanup; | ||
| } | ||
| printf("Server is listening on %s:%s\n", address, port); | ||
| while (1) { | ||
| struct sockaddr_storage client_address; | ||
| socklen_t client_address_len = sizeof(client_address); | ||
| int new_socket = accept(server_socket, (struct sockaddr *)&client_address, &client_address_len); | ||
| if (new_socket < 0) { | ||
| perror("Accept Error"); | ||
| continue; | ||
| } | ||
| pthread_t thread_id; | ||
| int *pclient = malloc(sizeof(int)); | ||
| if (pclient == NULL) { | ||
| perror("Malloc Error"); | ||
| close(new_socket); | ||
| continue; | ||
| } | ||
| *pclient = new_socket; | ||
| if (pthread_create(&thread_id, NULL, handle_client, pclient) != 0) { | ||
| perror("Thread Creation Error"); | ||
| close(new_socket); | ||
| free(pclient); | ||
| continue; | ||
| } else { | ||
| pthread_detach(thread_id); | ||
| } | ||
| } | ||
|
|
||
| cleanup: | ||
| if (res != NULL) { | ||
| freeaddrinfo(res); | ||
| } | ||
| if (server_socket != -1) { | ||
| close(server_socket); | ||
| } | ||
| exit(1); | ||
| } | ||
|
|
||
| void *handle_client(void *arg) { | ||
| int client_socket = *((int *)arg); | ||
| free(arg); | ||
| 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) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 標準出力に対して libc (printf など) と syscall を混ぜて使うと、 |
||
| perror("Write Error"); | ||
| break; | ||
| } | ||
| } | ||
| if (bytes_read < 0) { | ||
| perror("Read Error"); | ||
| } | ||
| close(client_socket); | ||
| return NULL; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
freeaddrinfo した後に res=NULL してあげて、2回目の解放を防ぐ。