可以在使用C语言实现Socket编程的两台计算机之间转移数据。
在相同情况下,可以使用用户数据报协议(UDP)和简单的客户端/服务器轻松地发送文件。
安全性-通过加密处理。
协议-UDP
加密-XOR加密
服务器启动并等待文件名。
客户端发送文件名。
服务器接收到该文件名。如果存在文件,则服务器开始读取文件,并继续发送一个缓冲区,该缓冲区填充有加密的文件内容,直到达到文件末尾为止。
文件尾标有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发送RecvFlag 0 #define NoFile "文件未找到!" //清除缓冲区功能 void clearBuf(char* b1){ int i; for (i = 0; i < Net_Buf_Size; i++) b1[i] = '\0'; } //加密方法功能 char Cipher(char ch1){ return ch1 ^ CipherKey; } //发送文件功能 int发送File(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; } //驱动程式码 int 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("\nfile descriptor is not接收d!!\n"); else printf("\nfile descriptor %d is接收d\n", sockfd1); // bind() if (bind(sockfd1, (struct sockaddr*)&addr_con, sizeof(addr_con)) == 0) printf("\nSuccessfully is binded!\n"); else printf("\nBinding is Failed!\n"); while (1) { printf("\nWaiting for name of file...\n"); //接收文件名 clearBuf(net_buf1); nBytes = recvfrom(sockfd1, net_buf1, Net_Buf_Size,发送RecvFlag, (struct sockaddr*)&addr_con, &addrlen); fp1 = fopen(net_buf1, "r"); printf("\nFile Name is接收d: %s\n", net_buf1); if (fp1 == NULL) printf("\nFile open is failed!\n"); else printf("\nFile Successfully is opened!\n"); while (1) { //处理 if (sendFile(fp1, net_buf1, Net_Buf_Size)) { 发送to(sockfd1, net_buf1, Net_Buf_Size, 发送RecvFlag, (struct sockaddr*)&addr_con, addrlen); break; } //发送 发送to(sockfd1, net_buf1, Net_Buf_Size, 发送RecvFlag, (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发送RecvFlag 0 //清除缓冲区功能 void clearBuf(char* b1){ int i; for (i = 0; i < Net_Buf_Size; i++) b1[i] = '\0'; } //解密方法功能 char Cipher(char ch1){ return ch1 ^ CipherKey; } //接收文件功能 int 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; } //驱动程式码 int 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("\nfile descriptor is not接收d!!\n"); else printf("\nfile descriptor %d is接收d\n", sockfd1); while (1) { printf("\nPlease enter the name of file to接收:\n"); scanf("%s", net_buf1); 发送to(sockfd1, net_buf1, Net_Buf_Size, 发送RecvFlag, (struct sockaddr*)&addr_con, addrlen); printf("\n---------Data is接收d---------\n"); while (1) { //接收 clearBuf(net_buf1); nBytes = recvfrom(sockfd1, net_buf1, Net_Buf_Size, 发送RecvFlag, (struct sockaddr*)&addr_con, &addrlen); //处理 if (recvFile(net_buf1, Net_Buf_Size)) { break; } } printf("\n-------------------------------\n"); } return 0; }