diff options
author | Ian C <ianc@noddybox.co.uk> | 2020-03-03 20:08:50 +0000 |
---|---|---|
committer | Ian C <ianc@noddybox.co.uk> | 2020-03-03 20:08:50 +0000 |
commit | 22a3f2e6e1796162542447f45a71bfd0e3288f3a (patch) | |
tree | 7f1d74718323df28e65742f6fdc4b91e4e548092 /server/nfts.c | |
parent | 3a5ef7e3652628339d790a0c9a40ae6081ee2770 (diff) |
Changed client to the server.
Diffstat (limited to 'server/nfts.c')
-rw-r--r-- | server/nfts.c | 176 |
1 files changed, 137 insertions, 39 deletions
diff --git a/server/nfts.c b/server/nfts.c index 18ab896..634f184 100644 --- a/server/nfts.c +++ b/server/nfts.c @@ -1,4 +1,4 @@ -/* Next File Transfer Client +/* Next File Transfer Server Copyright (C) 2020 Ian Cowburn This program is free software: you can redistribute it and/or modify @@ -82,32 +82,32 @@ static char *GetLine(char *buff, size_t len) } -static int Connect(const char *n, int p) +static int Socket(int p) { - struct hostent *remote; struct sockaddr_in addr; int sock; - if (!(remote = gethostbyname(n))) + if ((sock = socket(AF_INET,SOCK_STREAM,0))==-1) { - fprintf(stderr,"%s: unknown host %s\n",name,n); + perror("socket"); exit(EXIT_FAILURE); } - memcpy(&addr.sin_addr, remote->h_addr, remote->h_length); + /* Bind port to address + */ + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = INADDR_ANY; + addr.sin_port = htons(p); - if ((sock = socket(AF_INET, SOCK_STREAM, 0))==-1) + if (bind(sock, (void*)&addr, sizeof(addr))==-1) { - perror(name); + perror("bind"); exit(EXIT_FAILURE); } - addr.sin_family = AF_INET; - addr.sin_port = htons(p); - - if (connect(sock,(void*)&addr,sizeof(addr)) == -1) + if (listen(sock, 5)==-1) { - perror(name); + perror("listen"); exit(EXIT_FAILURE); } @@ -449,11 +449,60 @@ static int Mkdir(int sock, const char *path) } +static int Ls(int sock) +{ + int quit = 0; + char status[3] = {0}; + + if (!Write(sock, "LS", 2)) + { + quit = 1; + } + + if (!quit && Read(sock, status, 2)) + { + if (strcmp(status, "!E") == 0) + { + printf("Command failed\n"); + } + else if (strcmp(status, "OK") == 0) + { + int no; + int f; + char buff[1024]; + + no = ReadSize(sock); + + for(f = 0; f < no; f++) + { + int size; + + size = ReadSize(sock); + Read(sock, buff, size); + buff[size] = 0; + + printf("%s\n", buff); + } + } + else + { + printf("Unknown status '%s'\n", status); + } + } + else + { + quit = 1; + } + + return quit; +} + + int main(int argc, char *argv[]) { - char buff[1024]; int sock; - int quit = 0; + int port; + int quit; if ((name = strrchr(argv[0], '/'))) { @@ -464,43 +513,92 @@ int main(int argc, char *argv[]) name = argv[0]; } - if (argc<3) + if (argc<2) { - fprintf(stderr,"%s: usage %s host port\n",name,name); + fprintf(stderr, "%s: usage %s port\n", name, name); exit(EXIT_FAILURE); } - sock = Connect(argv[1],atoi(argv[2])); + port = atoi(argv[1]); + sock = Socket(port); - while(!quit && GetLine(buff, sizeof buff)) + quit = 0; + + while(!quit) { - char *tok; + char buff[1024]; + char *p; + int closed; + int fd; - tok = strtok(buff, " \t"); + printf("%s: listening on port %d\n", name, port); - if (strcmp(tok, "exit") == 0) - { - break; - } - else if (strcmp(tok, "put") == 0) - { - quit = Put(sock, strtok(NULL, " \t")); - } - else if (strcmp(tok, "get") == 0) - { - quit = Get(sock, strtok(NULL, " \t")); - } - else if (strcmp(tok, "cd") == 0) + if ((fd = accept(sock, NULL, 0)) == -1) { - quit = Cd(sock, strtok(NULL, " \t")); + perror("accept"); + exit(EXIT_FAILURE); } - else if (strcmp(tok, "mkdir") == 0) + + printf("%s: accepted connection\n", name); + + closed = 0; + + while(!closed && (p = GetLine(buff, sizeof buff))) { - quit = Mkdir(sock, strtok(NULL, " \t")); + char *tok; + + tok = strtok(buff, " \t"); + + if (strcmp(tok, "put") == 0) + { + closed = Put(fd, strtok(NULL, " \t")); + } + else if (strcmp(tok, "get") == 0) + { + closed = Get(fd, strtok(NULL, " \t")); + } + else if (strcmp(tok, "cd") == 0) + { + closed = Cd(fd, strtok(NULL, " \t")); + } + else if (strcmp(tok, "mkdir") == 0) + { + closed = Mkdir(fd, strtok(NULL, " \t")); + } + else if (strcmp(tok, "ls") == 0) + { + closed = Ls(fd); + } + else if (strcmp(tok, "close") == 0) + { + closed = 1; + } + else if (strcmp(tok, "exit") == 0) + { + closed = 1; + quit = 1; + } + else if (strcmp(tok, "help") == 0) + { + printf("put <file>\n"); + printf("get <file>\n"); + printf("cd <path>\n"); + printf("mkdir <path>\n"); + printf("ls\n"); + printf("close\n"); + printf("exit\n"); + } + else + { + printf("Unrecognised command '%s'\n", tok); + } } - else + + close(fd); + + if (!p) { - printf("Unrecognised command '%s'\n", tok); + quit = 1; } } |