
数据可以在两台使用 C 语言实现 Socket 编程的计算机之间传输。
在同样的情况下,可以轻松地通过实现用户数据报协议 (UDP) 和 简单的客户端/服务器。
安全性 - 通过加密处理。
协议 - UDP
加密 - 异或加密
算法
服务器启动并等待文件名。
-
客户端发送文件名。
该文件名由服务器接收。如果文件存在,服务器开始读取文件,并继续发送一个填充有加密文件内容的缓冲区,直到到达文件末尾。
文件结尾标记为EOF。
文件将作为缓冲区接收,直到且除非收到 EOF。之后对其进行加密。
如果文件不存在,则会发送一条消息“找不到文件”。
服务器
// server code for UDP socket programming
#include <arpa>
#include <netinet>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys>
#include <sys>
#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 buffer
void clearBuf(char* b1){
int i;
for (i = 0; i file descriptor is not received!!<p>");
else
printf("</p>
<p>file descriptor %d is received</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/ai/3788" title="MiniMax"><img
src="https://img.php.cn/upload/ai_manual/001/246/273/178599573262107.png" alt="MiniMax" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/ai/3788" title="MiniMax" class="overflowclass">MiniMax</a>
<p class="overflowclass">MiniMax是一款AI工具,推出的AI智能问答助手。</p>
</div>
<a rel="nofollow" href="/ai/3788" title="MiniMax" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div>
<p>", sockfd1);
// bind()
if (bind(sockfd1, (struct sockaddr*)&addr_con,
sizeof(addr_con)) == 0)
printf("</p>
<p>Successfully is binded!</p>
<p>");
else
printf("</p>
<p>Binding is Failed!</p>
<p>");
while (1) {
printf("</p>
<p>Waiting for name of file...</p>
<p>");
// 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("</p>
<p>File Name is Received: %s</p>
<p>", net_buf1);
if (fp1 == NULL)
printf("</p>
<p>File open is failed!</p>
<p>");
else
printf("</p>
<p>File Successfully is opened!</p>
<p>");
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;
}
</p></unistd.h></sys></sys></string.h></stdlib.h></stdio.h></netinet></arpa>
客户端
// client code for UDP socket programming
#include <arpa>
#include <netinet>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys>
#include <sys>
#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 buffer
void clearBuf(char* b1){
int i;
for (i = 0; i file descriptor is not received!!<p>");
else
printf("</p>
<p>file descriptor %d is received</p>
<p>", sockfd1);
while (1) {
printf("</p>
<p>Please enter the name of file to receive:</p>
<p>");
scanf("%s", net_buf1);
sendto(sockfd1, net_buf1, Net_Buf_Size,
SendRecvFlag, (struct sockaddr*)&addr_con,
addrlen);
printf("</p>
<p>---------Data is Received---------</p>
<p>");
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("</p>
<p>-------------------------------</p>
<p>");
}
return 0;
}</p></unistd.h></sys></sys></string.h></stdlib.h></stdio.h></netinet></arpa>










