您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

使用UDP进行文件传输的C程序

2024/4/1 4:28:52发布18次查看
数据可以在两台使用 c 语言实现 socket 编程的计算机之间传输。
在同样的情况下,可以轻松地通过实现用户数据报协议 (udp) 和简单的客户端/服务器。
安全性 - 通过加密处理。
协议 - udp
加密 - 异或加密
算法服务器启动并等待文件名。
客户端发送文件名。
该文件名由服务器接收。如果文件存在,服务器开始读取文件,并继续发送一个填充有加密文件内容的缓冲区,直到到达文件末尾。
文件结尾标记为eof。
文件将作为缓冲区接收,直到且除非收到 eof。之后对其进行加密。
如果文件不存在,则会发送一条消息“找不到文件”。
服务器// server code for udp socket programming#include <arpa/inet.h>#include <netinet/in.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/socket.h>#include <sys/types.h>#include <unistd.h>#define ip_protocol 0#define port_no 15050#define net_buf_size 32#define cipherkey 's'#define sendrecvflag 0#define nofile "file not found!"// function for clearing buffervoid clearbuf(char* b1){ int i; for (i = 0; i < net_buf_size; i++) b1[i] = '\0';}// function for encryption methodchar cipher(char ch1){ return ch1 ^ cipherkey;}// function for sending fileint sendfile(file* fp1, char* buf1, int s1){ int i, len; if (fp1 == null) { strcpy(buf1, nofile); len = strlen(nofile); buf1[len] = eof; for (i = 0; i <= len; i++) buf1[i] = cipher(buf1[i]); return 1; } char ch1, ch2; for (i = 0; i < s1; i++) { ch1= fgetc(fp); ch2 = cipher(ch1); buf1[i] = ch2; if (ch1 == eof) return 1; } return 0;}// driver codeint main(){ int sockfd1, nbytes; struct sockaddr_in addr_con; int addrlen = sizeof(addr_con); addr_con.sin_family = af_inet; addr_con.sin_port = htons(port_no); addr_con.sin_addr.s_addr = inaddr_any; char net_buf1[net_buf_size]; file* fp1; // socket() sockfd1 = socket(af_inet, sock_dgram, ip_protocol); if (sockfd1 < 0) printf("
file descriptor is not received!!
"); else printf("
file descriptor %d is received
", sockfd1); // bind() if (bind(sockfd1, (struct sockaddr*)&addr_con, sizeof(addr_con)) == 0) printf("
successfully is binded!
"); else printf("
binding is failed!
"); while (1) { printf("
waiting for name of file...
"); // receive name of file clearbuf(net_buf1); nbytes = recvfrom(sockfd1, net_buf1, net_buf_size, sendrecvflag, (struct sockaddr*)&addr_con, &addrlen); fp1 = fopen(net_buf1, "r"); printf("
file name is received: %s
", net_buf1); if (fp1 == null) printf("
file open is failed!
"); else printf("
file successfully is opened!
"); while (1) { // process if (sendfile(fp1, net_buf1, net_buf_size)) { sendto(sockfd1, net_buf1, net_buf_size, sendrecvflag, (struct sockaddr*)&addr_con, addrlen); break; } // send sendto(sockfd1, net_buf1, net_buf_size, sendrecvflag, (struct sockaddr*)&addr_con, addrlen); clearbuf(net_buf1); } if (fp1 != null) fclose(fp1); }return 0;}
客户端// client code for udp socket programming#include <arpa/inet.h>#include <netinet/in.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/socket.h>#include <sys/types.h>#include <unistd.h>#define ip_protocol 0#define ip_address "127.0.0.1" // localhost#define port_no 15050#define net_buf_size 32#define cipherkey 's'#define sendrecvflag 0// function for clearing buffervoid clearbuf(char* b1){ int i; for (i = 0; i < net_buf_size; i++) b1[i] = '\0';}// function for decryption methodchar cipher(char ch1){ return ch1 ^ cipherkey;}// function for receiveing fileint recvfile(char* buf1, int s1){ int i; char ch1; for (i = 0; i < s1; i++) { ch1 = buf1[i]; ch1 = cipher(ch1); if (ch1 == eof) return 1; else printf("%c", ch1); } return 0;}// driver codeint main(){ int sockfd1, nbytes; struct sockaddr_in addr_con; int addrlen = sizeof(addr_con); addr_con.sin_family = af_inet; addr_con.sin_port = htons(port_no); addr_con.sin_addr.s_addr = inet_addr(ip_address); char net_buf1[net_buf_size]; file* fp1; // socket() sockfd1 = socket(af_inet, sock_dgram, ip_protocol); if (sockfd1 < 0) printf("
file descriptor is not received!!
"); else printf("
file descriptor %d is received
", sockfd1); while (1) { printf("
please enter the name of file to receive:
"); scanf("%s", net_buf1); sendto(sockfd1, net_buf1, net_buf_size, sendrecvflag, (struct sockaddr*)&addr_con, addrlen); printf("
---------data is received---------
"); while (1) { // receive clearbuf(net_buf1); nbytes = recvfrom(sockfd1, net_buf1, net_buf_size, sendrecvflag, (struct sockaddr*)&addr_con, &addrlen); // process if (recvfile(net_buf1, net_buf_size)) { break; } } printf("
-------------------------------
"); } return 0;}
以上就是使用udp进行文件传输的c程序的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product