c++文件传输
我在从客户端向服务器发送文件时遇到问题。当我发送文本文件时,一切正常。但是,当我尝试发送二进制文件时,我只收到文件的一部分,或者应用程序崩溃。我想要一些可以很好地进行二进制传输的代码。我使用 WinSock,并且 SOCKET
指向服务器,SOCKET
指向客户端。我已经尝试了几乎所有的方法,请帮忙。
感谢您的任何回复。
编辑: 我实际上没有来源。我需要帮助发送 exe 文件比发送 txt 有哪些变化。转换?我将其作为垃圾箱打开,但没有帮助。
编辑2: @thkala: 这是非常具体的。我想我犯了所有 3 个可能的错误,我使用了 getline() 并在 0 字节处终止,所以可能是这样。如果不是getline(),该用什么?我在任何库中打开以使用文件,我现在使用 fstream。
I have a problem sending a file from client to server. When I send text files it's alright. But when I try to send a binary file, I recieve only one part of file or the app crashes. I'd love some code, that works fine for binary transfer. I use WinSock and I have SOCKET
pointing at server and SOCKET
pointing at client. I already tried almost everything, please help a bit.
Thanx for any replies.
EDIT:
I have no source actualy. I need help what changes in sending exe files than in sendig txt. Conversion? I open it as bin, but it doesnt help.
EDIT2:
@thkala:
It was very specific. I think I did all 3 possible errors, i used getline() and terminated on 0 byte, so it may be this. If not getline(), what to use? Im open at any liblary to use files, I use fstream for now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
文本文件和二进制文件之间的一些显着差异可能会或可能不会让您感到困惑:
文本文件通常有行,由行终止符分隔。对二进制文件使用面向行的函数可能会以各种有趣的方式失败。
作为上一点的后续内容,不能保证二进制文件将包含哪怕一个行终止符。这种差异往往会破坏代码,这些代码无意中假设一行不能长于 X 个字符,然后根据该假设继续分配内存,然后再吞入整个输入。无论 X 有多大,总会有一个文件更大。顺便说一句,这样的代码通常也会在机器生成的包含很长行的文本文件上中断。
文本文件通常被假定不包含零字节,而二进制文件通常包含零字节。由于零字节是 C 风格字符串的终止指示符,因此当由面向文本的函数处理时,二进制内容往往会被破坏。即使是各种函数的宽字符版本也会中断,因为它们只期望特定位置出现零字节。
除非您发布一些代码,否则实际上不可能提供比这更具体的帮助。
A few significant differences between text and binary files, that may or may not trip you up:
Text files typically have lines, delimited by line terminators. Using line-oriented functions for binary files can fail in various interesting ways.
As a follow-up to the previous point, there is no guarantee that a binary file will ever contain even a single line terminator. This difference tends to break code that unwittingly assumes that a line cannot be longer than X characters, then goes on to allocate memory based on that assumption, before slurping-in the whole input. No matter how large X may be, there will always be a file that will be bigger. BTW, such code will also typically break on machine-generated text files with very long lines.
Text files are usually assumed to not contain zero bytes, while binary files very often do. Since the zero byte is the termination indicator for C-style strings, binary content tends to be mangled when processed by text-oriented functions. Even the wide-character versions of the various functions will break, since they only expect zero bytes in specific locations.
Unless you post some of your code, it is not really possible to provide more specific help than this.
使用
fread()
、ReadFile()
或类似函数将文件中的数据块读取到内存缓冲区中,send()
缓冲区通过套接字,然后根据需要重复,直到读取整个文件。如果您不介意使用 Microsoft 特定的功能,那么您也可以使用CreateFile()
打开文件,然后使用TransmitFile()
让 WinSock 发送文件为您准备了套接字,这样您就不必手动读取它。Use
fread()
,ReadFile()
or similar function to read a chunk of data from the file into a memory buffer,send()
the buffer over the socket, then repeat as needed until the entire file has been read. If you don't mind using Microsoft-specific functionality, then you can alternatively open the file withCreateFile()
and then useTransmitFile()
to have WinSock send the file over the socket for you so you don't have to read it manually.