1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <sys/socket.h> #include <sys/sendfile.h> #include <netinet/in.h> #include <arpa/inet.h> #include <fcntl.h>
#define MAXLINE 511
int tcp_connect(int af, char *servip, unsigned short port) { struct sockaddr_in servaddr; int s; if ((s = socket(af, SOCK_STREAM, 0)) < 0) return -1; bzero((char *)&servaddr, sizeof(servaddr)); servaddr.sin_family = af; inet_pton(AF_INET, servip, &servaddr.sin_addr); servaddr.sin_port = htons(port);
if (connect(s, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) return -1; return s; }
int main(int argc, char *argv[]) { struct sockaddr_in server; struct stat obj; int sock; char bufmsg[MAXLINE]; char buf[100], command[5], filename[MAXLINE], *f; char temp[20]; int k, size, status; int filehandle;
if (argc != 3) { printf("사용법 : %s server_ip port\n", argv[0]); exit(1); }
sock = tcp_connect(AF_INET, argv[1], atoi(argv[2])); if (sock == -1) { printf("tcp_connect fail"); exit(1); }
while (1) { printf("\033[1;33m명령어 : get, put, pwd, ls, cd, quit\n"); printf("\033[1;32mclient> "); fgets(bufmsg, MAXLINE, stdin); fprintf(stderr, "\033[97m"); if (!strcmp(bufmsg, "get\n")) { printf("다운로드할 파일 : "); scanf("%s", filename); fgets(temp, MAXLINE, stdin); strcpy(buf, "get "); strcat(buf, filename); send(sock, buf, 100, 0); recv(sock, &size, sizeof(int), 0); if (!size) { printf("파일이 없습니다\n"); continue; } f = malloc(size); recv(sock, f, size, 0); while (1) { filehandle = open(filename, O_CREAT | O_EXCL | O_WRONLY, 0666); if (filehandle == -1) sprintf(filename + strlen(filename), "_1"); else break; } write(filehandle, f, size, 0); close(filehandle); printf("다운로드 완료\n"); } else if (!strcmp(bufmsg, "put\n")) { printf("업로드할 파일 : "); scanf("%s", filename); fgets(temp, MAXLINE, stdin); filehandle = open(filename, O_RDONLY); if (filehandle == -1) { printf("파일이 없습니다.\n"); continue; } strcpy(buf, "put "); strcat(buf, filename); send(sock, buf, 100, 0); stat(filename, &obj); size = obj.st_size; send(sock, &size, sizeof(int), 0); sendfile(sock, filehandle, NULL, size); recv(sock, &status, sizeof(int), 0); if (status) printf("업로드 완료\n"); else printf("업로드 실패\n"); } else if (!strcmp(bufmsg, "pwd\n")) { strcpy(buf, "pwd"); send(sock, buf, 100, 0); recv(sock, buf, 100, 0); printf("--The path of the Remote Directory--\n%s", buf); } else if (!strcmp(bufmsg, "ls\n")) { strcpy(buf, "ls"); send(sock, buf, 100, 0); recv(sock, &size, sizeof(int), 0); f = malloc(size); recv(sock, f, size, 0); filehandle = creat("temp.txt", O_WRONLY); write(filehandle, f, size, 0); close(filehandle); printf("--The Remote Directory List--\n"); system("cat temp.txt"); } else if (!strcmp(bufmsg, "cd\n")) { strcpy(buf, "cd "); printf("이동할 경로 이름 : "); scanf("%s", buf + 3); fgets(temp, MAXLINE, stdin); send(sock, buf, 100, 0); recv(sock, &status, sizeof(int), 0); if (status) printf("경로 변경 완료\n"); else printf("경로 변경 실패\n"); } else if (!strcmp(bufmsg, "quit\n")) { strcpy(buf, "quit"); send(sock, buf, 100, 0); recv(sock, &status, 100, 0); if (status) { printf("서버를 닫는 중..\n"); exit(0); } printf("서버 닫기 실패\n"); } } }
|