接收socket的命令
我正在使用套接字以 TCP、流模式将数据从本地计算机发送到远程。 本地端的代码是:
// ----------- Local
send(sd, pData, iSize, 0); // send data
数据的大小约为1Mb,因此socket可能会将其分成多个数据包。 当我在远程端接收数据时,我必须单独接收数据,然后将它们组合在一起。 远程端的代码是:
// ----------- Remote : Receiving data
int iSizeThis(0);// size of a single separated data
static int iSizeAcc(0);//size of the total data I have already got.
static int iDataSize(0);// size of the original data.
// Get size
if (iDataSize <= 0)
{
if ( (iSizeThis = recv(cli_sd, (char*)&iDataSize, 4, MSG_PEEK)) == 0) {
....
} else if (iSizeThis == SOCKET_ERROR) {
....
} else {
// Allocates memory
if (iDataSize > 0)
pData = realloc(pData, iDataSize);
}
} else if (iSizeAcc < iDataSize){
// Get data.
// The size of the data is about 1Mb, so socket will divide it to several packets.
// I have to recieve the data separately, and then combine them together.
iSizeThis = recv(cli_sd, ((char*)pData) + iSizeAcc, iDataSize - iSizeAcc, 0);
iSizeAcc += iSizeThis;
//{// If I uncomment this block, the recieving order will be reversed. Why?????
// static int i(0);
// std::ostringstream oss;
// oss << i++ << "\n\n";
// oss << "iSizeThis : " << iSizeThis << "\n";
// oss << "iSizeAcc : " << iSizeAcc << "\n";
// oss << "iDataSize : " << iDataSize << "\n";
// ::MessageBoxA(this->GetSafeHwnd(), oss.str().c_str(), "---", 0);
//}
// If all the fragment are combined into pData, the save it to a file.
if (iSizeAcc >= iDataSize){
// Save to file
FILE * pFile;
pFile = fopen ("CCC.dat","wb");
if (pFile != NULL){
fwrite ( ((char*)pData)+4 , 1 , iDataSize-4 , pFile );
fclose (pFile);
}
iSizeAcc = 0;
iDataSize = 0;
}
}
奇怪的是。如果我取消注释远程端的消息块,接收顺序将相反。 因此,远程数据的结果的顺序不正确。
为什么? (我怎样才能得到每个片段的正确顺序?)
提前致谢。
I am using socket to send data from local machine to remote in TCP, stream mode.
The code in the local side is :
// ----------- Local
send(sd, pData, iSize, 0); // send data
The size of the data is about 1Mb, so socket might divide it to several packets.
While I am recieving the data on remote side, I have to recieve the data separately, and then combine them together.
The code in the remote side is :
// ----------- Remote : Receiving data
int iSizeThis(0);// size of a single separated data
static int iSizeAcc(0);//size of the total data I have already got.
static int iDataSize(0);// size of the original data.
// Get size
if (iDataSize <= 0)
{
if ( (iSizeThis = recv(cli_sd, (char*)&iDataSize, 4, MSG_PEEK)) == 0) {
....
} else if (iSizeThis == SOCKET_ERROR) {
....
} else {
// Allocates memory
if (iDataSize > 0)
pData = realloc(pData, iDataSize);
}
} else if (iSizeAcc < iDataSize){
// Get data.
// The size of the data is about 1Mb, so socket will divide it to several packets.
// I have to recieve the data separately, and then combine them together.
iSizeThis = recv(cli_sd, ((char*)pData) + iSizeAcc, iDataSize - iSizeAcc, 0);
iSizeAcc += iSizeThis;
//{// If I uncomment this block, the recieving order will be reversed. Why?????
// static int i(0);
// std::ostringstream oss;
// oss << i++ << "\n\n";
// oss << "iSizeThis : " << iSizeThis << "\n";
// oss << "iSizeAcc : " << iSizeAcc << "\n";
// oss << "iDataSize : " << iDataSize << "\n";
// ::MessageBoxA(this->GetSafeHwnd(), oss.str().c_str(), "---", 0);
//}
// If all the fragment are combined into pData, the save it to a file.
if (iSizeAcc >= iDataSize){
// Save to file
FILE * pFile;
pFile = fopen ("CCC.dat","wb");
if (pFile != NULL){
fwrite ( ((char*)pData)+4 , 1 , iDataSize-4 , pFile );
fclose (pFile);
}
iSizeAcc = 0;
iDataSize = 0;
}
}
The odd thing is. If I uncomment the message block on remote side, the recieving order will be reversed.
Thus, the result of the remote data is not in a correct order.
Why? (And how could I get the correct order of each fragment?)
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当 MessageBoxA 函数执行时,它将消息发送到您的窗口。无论您的线程是否期待它们,MessageBoxA 都会将它们分派给您。
While the MessageBoxA function is executing, it pumps messages to your window. Whether or not your thread was expecting them, MessageBoxA dispatched them to you.
在接收循环中调用 MessageBoxA(阻塞模式对话框)是一个根本上有缺陷的想法。如果您想查看这些值,请在调试器中运行它,将它们打印到对话框(例如文本字段),将它们输出到控制台或将它们转储到文件中。
Calling MessageBoxA (a blocking, modal dialog) in a receive loop is a fundamentally flawed idea. If you want to see the values, run it in a debugger, print them to a dialog (e.g. a text field), output them to the console or dump them to a file.