首页 > 后端开发 > C++ > 正文

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

PHPz
发布: 2023-09-13 16:45:02
转载
2097人浏览过

使用udp进行文件传输的c程序

数据可以在两台使用 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 buffer
void clearBuf(char* b1){
   int i;
   for (i = 0; i < Net_Buf_Size; i++)
   b1[i] = '\0';
}
// function for encryption method
char Cipher(char ch1){
   return ch1 ^ CipherKey;
}
// function for sending file
int 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 code
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("</p><p>file descriptor is not received!!</p><p>");
   else
      printf("</p><p>file descriptor %d is received</p>
                    <div class="aritcle_card">
                        <a class="aritcle_card_img" href="/ai/1817">
                            <img src="https://img.php.cn/upload/ai_manual/000/969/633/68b6cb8b7ef86975.png" alt="行者AI">
                        </a>
                        <div class="aritcle_card_info">
                            <a href="/ai/1817">行者AI</a>
                            <p>行者AI绘图创作,唤醒新的灵感,创造更多可能</p>
                            <div class="">
                                <img src="/static/images/card_xiazai.png" alt="行者AI">
                                <span>100</span>
                            </div>
                        </div>
                        <a href="/ai/1817" class="aritcle_card_btn">
                            <span>查看详情</span>
                            <img src="/static/images/cardxiayige-3.png" alt="行者AI">
                        </a>
                    </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;
}

登录后复制

客户端

// 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 buffer
void clearBuf(char* b1){
   int i;
   for (i = 0; i < Net_Buf_Size; i++)
   b1[i] = '\0';
}
// function for decryption method
char Cipher(char ch1){
   return ch1 ^ CipherKey;
}
// function for receiveing file
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;
}
// driver code
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("</p><p>file descriptor is not received!!</p><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;
}
登录后复制

以上就是使用UDP进行文件传输的C程序的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号