Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************ Client.c *************/
- /*
- Send a file over a socket.
- Interface:
- ./executable [<input_path> [<sever_hostname> [<port>]]]
- Defaults:
- - input_path: input.tmp
- - server_hostname: 127.0.0.1
- - port: 12345
- */
- #define _XOPEN_SOURCE 700
- #include <stdio.h>
- #include <stdlib.h>
- #include <arpa/inet.h>
- #include <fcntl.h>
- #include <netdb.h> /* getprotobyname */
- #include <netinet/in.h>
- #include <sys/stat.h>
- #include <sys/socket.h>
- #include <unistd.h>
- int main(int argc, char **argv) {
- char protoname[] = "tcp";
- struct protoent *protoent;
- char *file_path = "input.tmp";
- char *server_hostname = "127.0.0.1";
- char *server_reply = NULL;
- char *user_input = NULL;
- char buffer[BUFSIZ];
- in_addr_t in_addr;
- in_addr_t server_addr;
- int filefd;
- int sockfd;
- ssize_t i;
- ssize_t read_return;
- struct hostent *hostent;
- struct sockaddr_in sockaddr_in;
- unsigned short server_port = 12345;
- if (argc > 1) {
- file_path = argv[1];
- if (argc > 2) {
- server_hostname = argv[2];
- if (argc > 3) {
- server_port = strtol(argv[3], NULL, 10);
- }
- }
- }
- filefd = open(file_path, O_RDONLY);
- if (filefd == -1) {
- perror("open");
- exit(EXIT_FAILURE);
- }
- /* Get socket. */
- protoent = getprotobyname(protoname);
- if (protoent == NULL) {
- perror("getprotobyname");
- exit(EXIT_FAILURE);
- }
- sockfd = socket(AF_INET, SOCK_STREAM, protoent->p_proto);
- if (sockfd == -1) {
- perror("socket");
- exit(EXIT_FAILURE);
- }
- /* Prepare sockaddr_in. */
- hostent = gethostbyname(server_hostname);
- if (hostent == NULL) {
- fprintf(stderr, "error: gethostbyname(\"%s\")\n", server_hostname);
- exit(EXIT_FAILURE);
- }
- in_addr = inet_addr(inet_ntoa(*(struct in_addr*)*(hostent->h_addr_list)));
- if (in_addr == (in_addr_t)-1) {
- fprintf(stderr, "error: inet_addr(\"%s\")\n", *(hostent->h_addr_list));
- exit(EXIT_FAILURE);
- }
- sockaddr_in.sin_addr.s_addr = in_addr;
- sockaddr_in.sin_family = AF_INET;
- sockaddr_in.sin_port = htons(server_port);
- /* Do the actual connection. */
- if (connect(sockfd, (struct sockaddr*)&sockaddr_in, sizeof(sockaddr_in)) == -1) {
- perror("connect");
- return EXIT_FAILURE;
- }
- while (1) {
- read_return = read(filefd, buffer, BUFSIZ);
- if (read_return == 0)
- break;
- if (read_return == -1) {
- perror("read");
- exit(EXIT_FAILURE);
- }
- /* TODO use write loop: https://stackoverflow.com/questions/24259640/writing-a-full-buffer-using-write-system-call */
- if (write(sockfd, buffer, read_return) == -1) {
- perror("write");
- exit(EXIT_FAILURE);
- }
- }
- free(user_input);
- free(server_reply);
- close(filefd);
- exit(EXIT_SUCCESS);
- }
- /************ Server.c *************/
- // https://stackoverflow.com/questions/2014033/send-and-receive-a-file-in-socket-programming-in-linux-with-c-c-gcc-g
- // https://stackoverflow.com/questions/2014033/send-and-receive-a-file-in-socket-programming-in-linux-with-c-c-gcc-g
- // https://stackoverflow.com/questions/2014033/send-and-receive-a-file-in-socket-programming-in-linux-with-c-c-gcc-g
- /*
- Receive a file over a socket.
- Saves it to output.tmp by default.
- Interface:
- ./executable [<output_file> [<port>]]
- Defaults:
- - output_file: output.tmp
- - port: 12345
- */
- #define _XOPEN_SOURCE 700
- #include <stdio.h>
- #include <stdlib.h>
- #include <arpa/inet.h>
- #include <fcntl.h>
- #include <netdb.h> /* getprotobyname */
- #include <netinet/in.h>
- #include <sys/stat.h>
- #include <sys/socket.h>
- #include <unistd.h>
- int main(int argc, char **argv) {
- char *file_path = "output.tmp";
- char buffer[BUFSIZ];
- char protoname[] = "tcp";
- int client_sockfd;
- int enable = 1;
- int filefd;
- int i;
- int server_sockfd;
- socklen_t client_len;
- ssize_t read_return;
- struct protoent *protoent;
- struct sockaddr_in client_address, server_address;
- unsigned short server_port = 12345u;
- if (argc > 1) {
- file_path = argv[1];
- if (argc > 2) {
- server_port = strtol(argv[2], NULL, 10);
- }
- }
- /* Create a socket and listen to it.. */
- protoent = getprotobyname(protoname);
- if (protoent == NULL) {
- perror("getprotobyname");
- exit(EXIT_FAILURE);
- }
- server_sockfd = socket(
- AF_INET,
- SOCK_STREAM,
- protoent->p_proto
- );
- if (server_sockfd == -1) {
- perror("socket");
- exit(EXIT_FAILURE);
- }
- if (setsockopt(server_sockfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) < 0) {
- perror("setsockopt(SO_REUSEADDR) failed");
- exit(EXIT_FAILURE);
- }
- server_address.sin_family = AF_INET;
- server_address.sin_addr.s_addr = htonl(INADDR_ANY);
- server_address.sin_port = htons(server_port);
- if (bind(
- server_sockfd,
- (struct sockaddr*)&server_address,
- sizeof(server_address)
- ) == -1
- ) {
- perror("bind");
- exit(EXIT_FAILURE);
- }
- if (listen(server_sockfd, 5) == -1) {
- perror("listen");
- exit(EXIT_FAILURE);
- }
- fprintf(stderr, "listening on port %d\n", server_port);
- while (1) {
- client_len = sizeof(client_address);
- puts("waiting for client");
- client_sockfd = accept(
- server_sockfd,
- (struct sockaddr*)&client_address,
- &client_len
- );
- filefd = open(file_path,
- O_WRONLY | O_CREAT | O_TRUNC,
- S_IRUSR | S_IWUSR);
- if (filefd == -1) {
- perror("open");
- exit(EXIT_FAILURE);
- }
- do {
- read_return = read(client_sockfd, buffer, BUFSIZ);
- if (read_return == -1) {
- perror("read");
- exit(EXIT_FAILURE);
- }
- if (write(filefd, buffer, read_return) == -1) {
- perror("write");
- exit(EXIT_FAILURE);
- }
- } while (read_return > 0);
- close(filefd);
- close(client_sockfd);
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement