当前位置:网站首页>C Implementation of Simple Network File Copy
C Implementation of Simple Network File Copy
2022-07-31 04:29:00 【mr bluetooth】
效果
服务端
./s
Client reads server file
./c 127.0.0.1 src_file dst_file 0
The client writes the file to the server
./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"); } }
<完>
边栏推荐
- Port inspection steps - 7680 port analysis - Dosvc service
- (五)final、抽象类、接口、内部类
- How Zotero removes auto-generated tags
- qlib自动化quant
- Win10 CUDA CUDNN 安装配置(torch paddlepaddle)
- Reinforcement learning: from entry to pit to shit
- 重磅 | 开放原子校源行活动正式启动
- BUG消灭者!!实用调试技巧超全整理
- "DeepJIT: An End-To-End Deep Learning Framework for Just-In-Time Defect Prediction" paper notes
- interprocess communication
猜你喜欢
Daily practice of LeetCode - palindrome structure of OR36 linked list
ENSP,划分VLAN、静态路由,三层交换机综合配置
Recursive implementation of the Tower of Hanoi problem
高等数学---第九章二重积分
[Paper reading] Mastering the game of Go with deep neural networks and tree search
STM32HAL库修改Hal_Delay为us级延时
Industry landing presents new progress | 2022 OpenAtom Global Open Source Summit OpenAtom OpenHarmony sub-forum was successfully held
exsl文件预览,word文件预览网页方法
VScode+ESP32 quickly install ESP-IDF plugin
(4) Recursion, variable parameters, access modifiers, understanding main method, code block
随机推荐
$parent/$children and ref
C language confession code?
开源汇智创未来 | 2022开放原子全球开源峰会OpenAtom openEuler分论坛圆满召开
The use of beforeDestroy and destroyed
RESTful api接口设计规范
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)
ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
Knowledge Distillation 7: Detailed Explanation of Knowledge Distillation Code
Pytest e-commerce project combat (on)
XSS shooting range (3) prompt to win
【wpf】wpf中的那些模板之深度解析
从零开始,一镜到底,纯净系统搭建除草机(Grasscutter)
ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
unity2d小游戏
MySQL数据库备份
prompt.ml/15中<svg>标签使用解释
npm、nrm两种方式查看源和切换镜像
Hand in hand to realize the picture preview plug-in (3)
Regarding the primary key id in the mysql8.0 database, when the id is inserted using replace to be 0, the actual id is automatically incremented after insertion, resulting in the solution to the repea
Basic knowledge of mysql (2)