本文主要是介绍山寨版tftp客户端,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
主要功能:
1. 上传文件到服务器
2. 从服务器上下载文件
3. 列出服务器上的文件
/*************************************************************************> File Name: client.c> Author: 联合作战实验室> Mail: v.manstein@qq.com> Created Time: Sat 25 Mar 2017 16:55:10 PM CST************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>#define err_log(errlog) do{printf("%s:%s,%d\n", __FILE__, __func__, __LINE__);\perror(errlog);exit(1);}while(0)#define N 128// ./client 192.168.8.211 10001int process_list(struct sockaddr_in serveraddr)
{int sockfd;char buf[N] = {};if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){err_log("failed to socket");}if(connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0){err_log("Failed to connect");}printf("Client connect success.\n");strcpy(buf, "L");if(send(sockfd, buf, N, 0) < 0){err_log("Failed to send");}while(recv(sockfd, buf,N, 0) > 0){printf("%s\n", buf);}close(sockfd);return 0;
}int process_get(struct sockaddr_in serveraddr, char * filename)
{int sockfd;char buf[N] = {};int fd;int nbyte;if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){err_log("failed to socket");}if(connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0){err_log("Failed to connect");}sprintf(buf, "G %s", filename); // G filenameif(send(sockfd, buf, N, 0) < 0){err_log("Failed to send");}if(recv(sockfd, buf, N, 0) < 0){err_log("Failed to recv");}if(buf[0] == 'N'){printf("%s is not exists!\n", filename);close(sockfd);return -1;}fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0664);if(fd < 0){close(sockfd);err_log("failed to open");}while((nbyte = recv(sockfd, buf, N, 0)) > 0){write(fd, buf, nbyte);}printf("Download done.\n");close(fd);close(sockfd);return 0;
}
int process_put(struct sockaddr_in serveraddr, char *filename)
{int sockfd;char buf[N] = {};int fd;int nbyte;if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){err_log("failed to socket");}if(connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0){err_log("Failed to connect");}sprintf(buf, "P %s", filename); // G filenameif(send(sockfd, buf, N, 0) < 0){err_log("Failed to send");}fd = open(filename, O_RDONLY);if(fd < 0){close(sockfd);err_log("failed to open");}usleep(50);while((nbyte = read(fd, buf, N)) > 0){send(sockfd, buf, nbyte, 0);usleep(50);}printf("upload done.\n");close(fd);close(sockfd);return 0;
}
int process_help(void)
{printf("************************************************************\n")printf("get file: download file form server.\n");printf("put file: upload file to server.\n");printf("list: list all file on server.\n");printf("help: print help info.\n");printf("************************************************************\n") return 0;}int main(int argc, const char *argv[])
{char buf[N] = {};struct sockaddr_in serveraddr, clientaddr;socklen_t addrlen = sizeof(struct sockaddr_in);if(argc < 3){fprintf(stderr,"Usage:%s serverip port.\n", argv[0]);return -1;}serveraddr.sin_family = AF_INET ;serveraddr.sin_addr.s_addr = inet_addr(argv[1]) ;serveraddr.sin_port = htons(atoi(argv[2]));while(1){printf("Input:");fgets(buf, N, stdin);buf[strlen(buf)-1] = '\0';// listif(strncmp(buf, "list", 4) == 0) // list all files{process_list(serveraddr);}else if(strncmp(buf, "get", 3) == 0) // get filename{process_get(serveraddr, buf+4);}else if(strncmp(buf, "put", 3) == 0) // put filename{process_put(serveraddr, buf+4);}else if(strncmp(buf, "help", 3) == 0) // help{process_help();}else if(strncmp(buf, "quit", 3) == 0) // quit{break;}else{printf("Invalid cmd!\n");}}return 0;
}
这篇关于山寨版tftp客户端的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!