当前位置:网站首页>简易网络文件拷贝的C实现
简易网络文件拷贝的C实现
2022-07-31 04:19:00 【蓝牙先生】
效果
服务端
./s
客户端读服务端文件
./c 127.0.0.1 src_file dst_file 0
客户端写文件到服务端
./c 127.0.0.1 src_file dst_file 1
服务端实现server.c
#include <stdio.h> #include <stdbool.h> #include <sys/types.h> #include <sys/socket.h> #include <stdint.h> #include <arpa/inet.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include "cs.c" int init_socket(void) { int opt = 1; struct sockaddr_in servaddr = {0}; unsigned int len = sizeof(opt); int sock = socket(AF_INET, SOCK_STREAM, 0); bool ok = false; servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERVICE_PORT); do { if(sock >= 0) { /* null */ } else { perror("socket"); break; } if(!setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, len)) { /* null */ } else { perror("setsockopt"); break; } if (!bind(sock, (struct sockaddr *)&servaddr,sizeof(servaddr))) { /* null */ } else { perror("bind"); break; } if (listen(sock, 1) == 0) { /* null */ } else { perror("listen"); break; } ok = true; } while(0); if(ok) { /* null */ } else if(sock >= 0) { close(sock); } return ok ? sock : -1; } int main(int argc, char *argv[]) { int client = -1; int socklen = sizeof(struct sockaddr_in); struct sockaddr_in clientaddr = {0}; int sock = init_socket(); while(sock >= 0) { client = accept(sock,(struct sockaddr *)&clientaddr,(socklen_t*)&socklen); if(client > 0) { printf("detect client\n"); } else { perror("accept"); continue; } op.magic = 0; if (recv(client, &op,sizeof(op), 0) == sizeof(op)) { /* null */ } else { printf("recv op err\n"); } if(op.magic != START_EXIT) { printf("op.magic != START_EXIT\n"); } else { switch(op.read_write) { case 0: printf("client read file:%s -> %s\n", op.src_file, op.dst_file); client_read_file(client, (char *)op.src_file); break; case 1: printf("client write file:%s -> %s\n", op.src_file, op.dst_file); client_write_file(client, (char *)op.dst_file); break; default: break; } } close(client); } return 0; } void client_read_file(int client, char *filename) { // server read to -> client __read(client, filename); } void client_write_file(int client, char *filename) { // client send to -> server __write(client, filename); }
客户端实现client.c
#include <stdio.h> #include <stdbool.h> #include <sys/types.h> #include <sys/socket.h> #include <stdint.h> #include <arpa/inet.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include "cs.c" int init_socket(int argc, char *argv[]) { int sock = socket(AF_INET, SOCK_STREAM, 0); int opt = 1; unsigned int len = sizeof(opt); struct sockaddr_in servaddr = {0}; bool ok = false; servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = inet_addr(argv[1]); servaddr.sin_port = htons(SERVICE_PORT); do { if(sock >= 0) { /* null */ } else { perror("socket"); break; } if(!setsockopt(sock , SOL_SOCKET, SO_REUSEADDR, &opt, len)) { /* null */ } else { perror("setsockopt"); break; } if(!connect(sock, (struct sockaddr *)&servaddr, sizeof(servaddr))) { /* null */ } else { perror("connect"); break; } ok = true; } while(0); if(ok) { /* null */ } else if(sock >= 0){ close(sock); } return ok ? sock : -1; } int main(int argc, char *argv[]) { int sock = init_socket(argc, argv); memcpy(op.src_file, argv[2], strlen(argv[2])); memcpy(op.dst_file, argv[3], strlen(argv[2])); op.read_write = atoi(argv[4]); op.magic = START_EXIT; if(send(sock, &op, sizeof(op), 0) != sizeof(op)) { perror("send"); } else { switch(op.read_write) { case 0: printf("client read file:%s -> %s\n", op.src_file, op.dst_file); client_read_file(sock, (char *)op.dst_file); break; case 1: printf("client write file:%s -> %s\n", op.src_file, op.dst_file); client_write_file(sock, (char *)op.src_file); break; default: break; } close(sock); } return 0; } void client_read_file(int client, char *filename) { // server send to -> client __write(client, filename); } void client_write_file(int client, char *filename) { // client send to -> server __read(client, filename); }
文件读写实现cs.c
#define READ_WRITE_END 0xBB #define START_EXIT 0xCC #define SERVICE_PORT 1234 struct trans_op { uint8_t magic; uint8_t read_write;//0:read 1:write uint32_t size; uint8_t src_file[64]; uint8_t dst_file[64]; uint8_t buffer[512 - 8 - 128]; }; static struct trans_op op; void client_read_file(int client, char *filename); void client_write_file(int client, char *filename); void __write (int client, char *filename) { int fd = open(filename, O_CREAT | O_WRONLY, 0777); ssize_t res = 0; ssize_t total = 0; printf("write %s\n", filename); if(fd < 0) { perror("open"); } else { while(1) { memset(&op, 0, sizeof(op)); if((res = recv(client, &op, sizeof(op), 0)) != sizeof(op)) { perror("recv error"); break; } else if(op.magic == READ_WRITE_END) { printf("magic is bb exit.\n"); break; } else if(write(fd, op.buffer, op.size) == -1) { perror("write"); break; } else { total += op.size; } } printf("toatl = %ld\n", total); close(fd); } /* check return code */ if(op.magic != READ_WRITE_END || op.size != total) { printf("end fail\n"); } else { printf("end success\n"); } /* replay */ op.magic = START_EXIT; op.size = total; send(client, &op, sizeof(op), 0); } void __read(int client, char *filename) { int fd = open(filename, O_RDONLY, 0777); ssize_t total = 0; printf("read %s\n", filename); if(fd < 0) { perror("open"); } else { while(1) { memset(&op, 0, sizeof(op)); if((op.size = read(fd, op.buffer, sizeof(op.buffer))) <= 0 ) { perror("read end or error"); break; } else if(send(client, &op, sizeof(op),0) != sizeof(op)) { perror("send error"); break; } else { total += op.size; } } /* relply */ op.magic = READ_WRITE_END; op.size = total; if(send(client, &op, sizeof(op), 0) == sizeof(op)) { /* null */ } else { perror("send error"); } printf("toatl = %ld\n", total); close(fd); } /* check return code */ recv(client, &op, sizeof(op), 0); if(op.magic != START_EXIT || op.size != total) { printf("end fail, op.magic,op.size=%d,%d\n", op.magic, op.size); } else { printf("end success\n"); } }
<完>
边栏推荐
- How Zotero removes auto-generated tags
- [Swift]自定义点击APP图标弹出的快捷方式
- Error EPERM operation not permitted, mkdir ‘Dsoftwarenodejsnode_cache_cacach两种解决办法
- 开源汇智创未来 | 2022开放原子全球开源峰会OpenAtom openEuler分论坛圆满召开
- 慧通编程第4关 - 魔法学院第6课
- Know the showTimePicker method of the basic components of Flutter
- The application and practice of mid-to-platform brand advertising platform
- ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)
- Notes on the establishment of the company's official website (6): The public security record of the domain name is carried out and the record number is displayed at the bottom of the web page
- log level and print log note
猜你喜欢
微信小程序使用云函数更新和添加云数据库嵌套数组元素
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)
(4) Recursion, variable parameters, access modifiers, understanding main method, code block
ENSP,划分VLAN、静态路由,三层交换机综合配置
C language confession code?
No qualifying bean of type 问题
Safety 20220712
Zotero如何删除自动生成的标签
Port inspection steps - 7680 port analysis - Dosvc service
Knowledge Distillation 7: Detailed Explanation of Knowledge Distillation Code
随机推荐
type_traits元编程库学习
安全20220722
Redis uses sorted set to cache latest comments
[C language] General method for finding the sum of the greatest common factor and the least common multiple of two integers m and n, the classical solution
Basic knowledge of mysql (2)
ENSP, VLAN division, static routing, comprehensive configuration of Layer 3 switches
进程间通信
ENSP,划分VLAN、静态路由,三层交换机综合配置
IDEA常用快捷键与插件
SOLVED: After accidentally uninstalling pip (two ways to manually install pip)
interprocess communication
(Line segment tree) Summary of common problems of basic line segment tree
慧通编程第4关 - 魔法学院第6课
[Paper reading] Mastering the game of Go with deep neural networks and tree search
Vue项目通过node连接MySQL数据库并实现增删改查操作
组件传值 provide/inject
(四)递归、可变参数、访问修饰符、理解 main 方法、代码块
行业落地呈现新进展 | 2022开放原子全球开源峰会OpenAtom OpenHarmony分论坛圆满召开
强化学习:从入门到入坑再到拉屎
Safety 20220722