如何在C/C++中通过TCP发送.mp4文件

How to send .mp4 file over TCP in C/C++?

本文关键字:TCP 发送 mp4 文件 C++      更新时间:2024-04-28

我正在尝试通过C++应用程序中的TCP客户端/服务器发送文件。场景非常简单;客户端发送文件,服务器端接收文件。我可以成功发送基于文本的文件(如.cpp或.txt(,但当我尝试发送.mp4或.zip等文件时,服务器上收到的文件已损坏。

client.cpp

FILE *fptr = fopen(m_fileName.c_str(), "rb");
off_t offset = 0;
int bytes = 1;
if(fptr){
while (bytes > 0){
bytes = sendfile(m_sock, fptr->_fileno, &offset, BUFFER_SIZE);
std::cout<< "sended bytes : "<< offset << 'n';
}
}
fclose(fptr);
std::cout<< "File transfer completed!n";

Server.cpp

//some stuff...
for (;;) {
if ((m_result = recv(epes[i].data.fd, buf, BUFFER_SIZE, 0)) == -1) {
if (errno == EAGAIN)
break;
exit_sys("recv");
}

if (m_result > 0) {
buf[m_result] = '';

std::ofstream outfile;
if(!outfile.is_open())
outfile.open(m_filename.c_str(), std::ios_base::app | std::ios_base::binary);

outfile << std::unitbuf << buf;
m_filesize -= m_result;
std::cout << "count       : " << m_result << 'n';
std::cout << "remain data : " << m_filesize << 'n';
if(!m_filesize){
outfile.close();
m_fileTransferReady = 0;
std::cout<<"File transfer stopn";
if (send(epes[i].data.fd, "transferok", 10, 0) == -1)
exit_sys("send");
}
}
//some stuff for else
}

我在发送和接收文件时使用了二进制模式,但我想这还不够。格式化文件有特殊的发送方法吗?

我按照注释中提到的G.M.找到了解决方案。在以二进制模式打开的文件末尾添加null字符会导致非基于文本的文件出现问题。另一个问题是使用<lt;操作人员需要使用写成员函数来代替流。因此

服务器.cpp

//...
//...

if (m_result > 0) {
//buf[m_result] = '';       //Deleted!
std::ofstream outfile;
if(!outfile.is_open())
outfile.open(m_filename.c_str(), std::ios_base::app | std::ios_base::binary);

outfile << std::unitbuf;      //Modified
outfile.write(buf, m_result); //Added
m_filesize -= m_result;

//...
//...