Linux Socket C语言版大文件上传到服务器(二进制文件也可以)
2019-05-28 15:31
676 查看
客户端程序:client.c
include <stdio.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/wait.h> #include <sys/socket.h> #include <signal.h> #include <ctype.h> #include <arpa/inet.h> #include <netdb.h> #define PORT 20002 #define LENGTH 1024 void error(const char *msg) { perror(msg); exit(1); } int main(int argc, char *argv[]) { int sockfd; int nsockfd; char revbuf[LENGTH]; struct sockaddr_in remote_addr; if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { error("ERROR: Failed to obtain Socket Descriptor!\n"); } remote_addr.sin_family = AF_INET; remote_addr.sin_port = htons(PORT); inet_pton(AF_INET, "127.0.0.1", &remote_addr.sin_addr); bzero(&(remote_addr.sin_zero), 8); if (connect(sockfd, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr)) == -1) { error("ERROR: Failed to connect to the host!\n"); } else printf("[Client] Connected to server at port %d...ok!\n", PORT); //需要上传的文件路径及名称 char* fs_name = "/home/lyn/aaa.jpg"; char sdbuf[LENGTH]; printf("[Client] Sending %s to the Server...", fs_name); FILE *fs = fopen(fs_name, "rb"); if(fs == NULL) { printf("ERROR: File %s not found.\n", fs_name); exit(1); } bzero(sdbuf, LENGTH); int fs_block_sz; while((fs_block_sz = fread(sdbuf, sizeof(char), LENGTH, fs))>0) { if(send(sockfd, sdbuf, fs_block_sz, 0) < 0) { printf("ERROR: Failed to send file %s.\n", fs_name); break; } bzero(sdbuf, LENGTH); } printf("Ok File %s from Client was Sent!\n", fs_name); close (sockfd); printf("[Client] Connection lost.\n"); return (0); }
服务端程序:server.c
#include <stdlib.h> #include <stdio.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/wait.h> #include <sys/socket.h> #include <signal.h> #include <ctype.h> #include <arpa/inet.h> #include <netdb.h> #define PORT 20002 #define BACKLOG 5 #define LENGTH 512 void error(const char *msg) { perror(msg); exit(1); } int main () { int sockfd; int nsockfd; int num; int sin_size; struct sockaddr_in addr_local; struct sockaddr_in addr_remote; char revbuf[LENGTH]; if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ) { error("ERROR: Failed to obtain Socket Descriptor.\n"); } else printf("[Server] Obtaining socket descriptor successfully.\n"); addr_local.sin_family = AF_INET; addr_local.sin_port = htons(PORT); addr_local.sin_addr.s_addr = INADDR_ANY; bzero(&(addr_local.sin_zero), 8); if( bind(sockfd, (struct sockaddr*)&addr_local, sizeof(struct sockaddr)) == -1 ) { error("ERROR: Failed to bind Port.\n"); } else printf("[Server] Binded tcp port %d in addr 192.168.11.139 sucessfully.\n",PORT); if(listen(sockfd,BACKLOG) == -1) { error("ERROR: Failed to listen Port.\n"); } else printf ("[Server] Listening the port %d successfully.\n", PORT); int success = 0; while(success == 0) { sin_size = sizeof(struct sockaddr_in); if ((nsockfd = accept(sockfd, (struct sockaddr *)&addr_remote, &sin_size)) == -1) { error("ERROR: Obtaining new Socket Despcritor.\n"); } else printf("[Server] Server has got connected from %s.\n", inet_ntoa(addr_remote.sin_addr)); //要上传到服务器的位置 char* fr_name = "../image/aa.jpg"; FILE *fr = fopen(fr_name, "wb"); if(fr == NULL) printf("File %s Cannot be opened file on server.\n", fr_name); else { bzero(revbuf, LENGTH); int fr_block_sz = 0; while(fr_block_sz = recv(nsockfd, revbuf, LENGTH, 0)) { if(fr_block_sz < 0) { error("Error receiving file from client to server.\n"); } int write_sz = fwrite(revbuf, sizeof(char), fr_block_sz, fr); if(write_sz < fr_block_sz) { error("File write failed on server.\n"); } else if(fr_block_sz == -1)//如果不判断==-1的话只能写512个字节哦~ { break; } bzero(revbuf, LENGTH); } printf("Ok received from client!\n"); fclose(fr); } close(nsockfd); printf("[Server] Connection with Client closed. Server will wait now...\n"); while(waitpid(-1, NULL, WNOHANG) > 0); } }
相关文章推荐
- Linux 服务器 文件上传
- Linux C Socket编程:文件上传
- python上传字符和二进制的文件到PHP服务器
- CRT + lrzsz 进行远程linux系统服务器文件上传下载
- bat脚本自动上传文件到Linux FTP服务器上
- springMVC+ Linux + VSFTPD + nginx实现文件上传至图片服务器
- Linux-ftp服务器 文件的上传和下载总结(1-实名登录)
- linux 上tomcat 服务器抛出socket异常“文件打开太多”的问题 - 也碰到了这个问题,解决方案如下
- 使用TCP协议写一个可以上传文件的服务器和客户端。
- 使用TCP协议写一个可以上传文件的服务器和客户端
- ftp+nginx搭建图片服务器,上传FTP文件可以传到服务器上,但是文件大小是0字节的 而且点击按钮后程序会卡死
- mac os x 怎么上传文件到 linux 服务器上
- 如何上传文件到阿里云服务器linux
- linux定时上传本地文件到ftp服务器shell脚本
- PHP-Websockets 上传文件2 优化支持php socket客户端和websocket连接websocket服务器 以守护进程方式运行编码
- Windows中的文件上传到Linux的ftp服务器的方法
- android连接socket服务器上传下载多个文件
- linux集群服务器文件上传大小配置
- JAVA_ 网络编程,写一个可以上传文件的服务器和客户端
- Linux系统安装rz/sz实现本地和服务器文件上传下载