diff options
author | Niklas Halle <niklas@niklashalle.net> | 2020-06-07 12:51:27 +0200 |
---|---|---|
committer | Niklas Halle <niklas@niklashalle.net> | 2020-06-07 12:51:27 +0200 |
commit | 9629104f494a441681fd28295f98531682875fc7 (patch) | |
tree | 8dc7069a149880fcadc38050df34c128e06d5be1 /03_exercise/cli | |
parent | 2821f125c91c192cdd28997e78f366f2db2d39c4 (diff) | |
download | betriebssysteme-9629104f494a441681fd28295f98531682875fc7.tar.gz betriebssysteme-9629104f494a441681fd28295f98531682875fc7.zip |
put works
Diffstat (limited to '03_exercise/cli')
-rwxr-xr-x | 03_exercise/cli/client | bin | 28384 -> 28360 bytes | |||
-rw-r--r-- | 03_exercise/cli/client.c | 41 |
2 files changed, 22 insertions, 19 deletions
diff --git a/03_exercise/cli/client b/03_exercise/cli/client Binary files differindex 0fcf5c1..34d1d01 100755 --- a/03_exercise/cli/client +++ b/03_exercise/cli/client diff --git a/03_exercise/cli/client.c b/03_exercise/cli/client.c index c1bc5d1..052dabd 100644 --- a/03_exercise/cli/client.c +++ b/03_exercise/cli/client.c @@ -50,36 +50,38 @@ int write_all(int sock, char *buffer, int size) { } void send_file(int server_fd, char *path) { - char buffer[512]; - int file_fd; - - if ((file_fd = open(path, O_RDONLY)) == -1) { perror("Open"); } - - FILE *file = fdopen(file_fd, "r"); + printf("Trying to send \"%s\" to remote...\n", path); + char buffer[BUF_SIZE]; + memset(buffer, 0, BUF_SIZE); + int tmp_fd, file_fd; + if ((tmp_fd = open(path, O_RDONLY)) == -1) { perror("Open"); } + FILE *file = fdopen(tmp_fd, "r"); fseek(file, 0L, SEEK_END); long int sz = ftell(file); fseek(file, 0L, SEEK_SET); + fclose(file); + close(tmp_fd); + if ((file_fd = open(path, O_RDONLY)) == -1) { perror("Open"); } int length = snprintf(NULL, 0, "%ld", sz); - char msg[length + 1 + 3]; - sprintf(msg, "<<!%ld", sz); + sprintf(buffer, "<<!%ld!", sz); + sprintf(&(buffer[3 + length + 1]), "%s\n", path); - write(server_fd, msg, strlen(msg)); - - int size; - while (read(file_fd, buffer, sizeof(buffer)) != 0) { - size = strlen(buffer) + 1; - if (write(server_fd, &size, sizeof(size)) < 0) { - perror("Write"); - exit(1); - } + write(server_fd, buffer, strlen(buffer)); + memset(buffer, 0, BUF_SIZE); + errno = 0; + while (read(file_fd, buffer, sizeof(buffer)) > 0) { printf("Sent %i bytes\n", write_all(server_fd, buffer, strlen(buffer) + 1)); } - fclose(file); + if (errno) + perror("wad"); + close(file_fd); + + printf("done.\n"); } #define BREAK 1 @@ -143,7 +145,7 @@ int main() { pid_t pid = fork(); int done = 0; - if (pid == 0) { + if (pid != 0) { read_stdin(&done); } else { read_server_sock(&done); @@ -175,6 +177,7 @@ void read_stdin(int *done) { ssize_t length; while (!*done) { + memset(stdinp_buffer, 0, BUF_SIZE); if ((length = read(STDIN_FILENO, stdinp_buffer, BUF_SIZE)) > 0) { switch (parse(sock, stdinp_buffer, length)) { case CONTINUE: |