c++ udp传文件 例子
时间: 2023-10-09 14:17:36 浏览: 93
C++ TCP-UDP传文件例子
以下是一个简单的 C++ UDP 传输文件的示例代码:
发送端:
```c++
#include <iostream>
#include <fstream>
#include <cstring>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#define MAX_BUFFER_SIZE 1024
using namespace std;
int main() {
int sockfd;
struct sockaddr_in addr;
char buffer[MAX_BUFFER_SIZE];
int file_size, bytes_sent, bytes_read;
ifstream file;
socklen_t addr_len = sizeof(addr);
// 创建 socket
sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sockfd == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
// 设置目标地址
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(8888);
inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
// 打开文件并获取文件大小
file.open("test.txt", ios::in | ios::binary);
if (!file.is_open()) {
perror("open file");
exit(EXIT_FAILURE);
}
file.seekg(0, ios::end);
file_size = file.tellg();
file.seekg(0, ios::beg);
// 发送文件大小
bytes_sent = sendto(sockfd, &file_size, sizeof(file_size), 0, (struct sockaddr*)&addr, addr_len);
if (bytes_sent == -1) {
perror("sendto");
exit(EXIT_FAILURE);
}
// 发送文件内容
while (file.read(buffer, MAX_BUFFER_SIZE)) {
bytes_sent = sendto(sockfd, buffer, MAX_BUFFER_SIZE, 0, (struct sockaddr*)&addr, addr_len);
if (bytes_sent == -1) {
perror("sendto");
exit(EXIT_FAILURE);
}
}
bytes_read = file.gcount();
if (bytes_read > 0) {
bytes_sent = sendto(sockfd, buffer, bytes_read, 0, (struct sockaddr*)&addr, addr_len);
if (bytes_sent == -1) {
perror("sendto");
exit(EXIT_FAILURE);
}
}
// 关闭文件和 socket
file.close();
close(sockfd);
return 0;
}
```
接收端:
```c++
#include <iostream>
#include <fstream>
#include <cstring>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#define MAX_BUFFER_SIZE 1024
using namespace std;
int main() {
int sockfd;
struct sockaddr_in addr;
char buffer[MAX_BUFFER_SIZE];
int file_size, bytes_received, bytes_written;
ofstream file;
socklen_t addr_len = sizeof(addr);
// 创建 socket
sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sockfd == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
// 绑定端口
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(8888);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sockfd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
perror("bind");
exit(EXIT_FAILURE);
}
// 接收文件大小
bytes_received = recvfrom(sockfd, &file_size, sizeof(file_size), 0, (struct sockaddr*)&addr, &addr_len);
if (bytes_received == -1) {
perror("recvfrom");
exit(EXIT_FAILURE);
}
// 接收文件内容并写入文件
file.open("test2.txt", ios::out | ios::binary);
if (!file.is_open()) {
perror("open file");
exit(EXIT_FAILURE);
}
while (bytes_received = recvfrom(sockfd, buffer, MAX_BUFFER_SIZE, 0, (struct sockaddr*)&addr, &addr_len)) {
if (bytes_received == -1) {
perror("recvfrom");
exit(EXIT_FAILURE);
}
bytes_written = file.write(buffer, bytes_received).tellp();
if (bytes_written == -1) {
perror("write file");
exit(EXIT_FAILURE);
}
if (bytes_written == file_size) {
break;
}
}
// 关闭文件和 socket
file.close();
close(sockfd);
return 0;
}
```
需要注意的是,此示例代码并没有进行错误处理和传输完整性检查,主要为了演示 UDP 文件传输的基本原理。在实际使用中,应该增加必要的错误处理和传输完整性检查。
阅读全文