当前位置:网站首页>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"); } }
<完>
边栏推荐
- 行业落地呈现新进展 | 2022开放原子全球开源峰会OpenAtom OpenHarmony分论坛圆满召开
- 【AUTOSAR-RTE】-4-Port and Interface and Data Type
- PWN ROP
- 开源汇智创未来 | 2022开放原子全球开源峰会OpenAtom openEuler分论坛圆满召开
- unity2d小游戏
- (Line segment tree) Summary of common problems of basic line segment tree
- open failed: EACCES (Permission denied)
- 三子棋的代码实现
- type_traits metaprogramming library learning
- The use of beforeDestroy and destroyed
猜你喜欢

马斯克对话“虚拟版”马斯克,脑机交互技术离我们有多远

C language from entry to such as soil, the data store

Daily practice of LeetCode - 138. Copy a linked list with random pointers

mysql基础知识(二)

BP神经网络

qlib架构

(五)final、抽象类、接口、内部类

WPF WPF 】 【 the depth resolution of the template

SOLVED: After accidentally uninstalling pip (two ways to manually install pip)
![[Swift] Customize the shortcut that pops up by clicking the APP icon](/img/d4/84b237995fc3d3700916eb57f6670d.png)
[Swift] Customize the shortcut that pops up by clicking the APP icon
随机推荐
Two address pools r2 are responsible for managing the address pool r1 is responsible for managing dhcp relays
Safety 20220712
已解决:不小心卸载pip后(手动安装pip的两种方式)
[Swift] Customize the shortcut that pops up by clicking the APP icon
C语言表白代码?
log level and print log note
three.js make 3D photo album
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)
(树) 最近公共祖先(LCA)
Industry landing presents new progress | 2022 OpenAtom Global Open Source Summit OpenAtom OpenHarmony sub-forum was successfully held
(tree) Last Common Ancestor (LCA)
Can't load /home/Iot/.rnd into RNG
The BP neural network
行业落地呈现新进展 | 2022开放原子全球开源峰会OpenAtom OpenHarmony分论坛圆满召开
mysql数据库安装(详细)
《DeepJIT: An End-To-End Deep Learning Framework for Just-In-Time Defect Prediction》论文笔记
qlib自动化quant
"A daily practice, happy water problem" 1331. Array serial number conversion
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
(八)Math 类、Arrays 类、System类、Biglnteger 和 BigDecimal 类、日期类