2014-11-05 143 views
1

我正在学习boost :: asio网络编程,并试图进行简单的文件传输练习,使用阻塞套接字,所以,困在奇怪的问题问题。使用boost :: asio编写简单的文件传输程序。有主要发送接收desync

服务器(接收器)循环如下:

while (true){ 
    int bufSize{ static_cast<int>(pow(2, 18)) }; 
    char* buf{ new char[bufSize] }; 

    __int64 currPos{ 0 }; 
    __int64 fileSize; 
    std::string fileName; 
    mSocket->receive(buffer(buf, bufSize)); // here we get pre-defined packet with XML 
    ParseDescXML(std::string{ buf }, &fileName, &fileSize); // and parse this XML, get name and size 

    std::ofstream ofs(mSavePath + fileName, std::ios::binary); 
    if (ofs.is_open()){ 
     while (currPos != fileSize) { 
      if (bufSize > fileSize - currPos){ 
       delete[] buf; 
       bufSize = fileSize - currPos; 
       buf = new char[bufSize]; 
      } 
      mSocket->receive(buffer(buf, bufSize)); 
      ofs.write(buf, bufSize); 
      currPos += bufSize; 
      std::cout << "SERVER " << currPos << std::endl; 
     } 
    } 

    delete[] buf; 
    ofs.close(); 
    slDebug("Receive completed"); // output some stuff, not related to issue 
} 

客户端(发送器)循环如下:

mWorkerOccupied = true; 
std::ifstream ifs(filePath, std::ios::binary); 

if (!ifs.is_open()){ 
    mWorkerOccupied = false; 
    return false; 
} 

mFileName = filePath.substr(filePath.find_last_of('\\') + 1, filePath.length()); 
mCurrPos = 0; 
mFileSize = GetFileSize(&ifs); 

std::string xmlDesc{ MakeXMLFileDesc(mFileName, mFileSize) }; // here we make XML description 
xmlDesc.push_back('\0'); 

int bufSize{ static_cast<int>(pow(2, 18)) }; 
char* buf{ new char[bufSize] }; 

mSocket->send(buffer(xmlDesc.c_str(), bufSize)); // and send it. 

while (mCurrPos != mFileSize){ 
    if (bufSize > mFileSize - mCurrPos){ 
     delete[] buf; 
     bufSize = mFileSize - mCurrPos; 
     buf = new char[bufSize]; 
    } 
    ifs.read(buf, bufSize); 
    mSocket->send(buffer(buf, bufSize)); 
    mCurrPos += bufSize; 

    std::cout << "CLIENT " << mCurrPos << std::endl; 
} 

ifs.close(); 
delete[] buf; 
mWorkerOccupied = false; 

slDebug("SendFile completed"); 

所有这些东西是在平行线程中运行。 从我的理解应该以这种方式工作:

  1. 服务器的线程中运行服务器和挂起,直到传入的连接(如预期工作,所以我在这里没有包含这些代码)。
  2. 客户线程一段时间后运行并连接到服务器(按预期工作)
  3. 服务器等待第一个数据包,包含XML(按预期工作)
  4. 客户端发送XML,服务器得到它(按预期工作)
  5. 客户端开始发送实际的二进制数据,服务器得到它。这里我们遇到重大问题。

我在客户端和服务器端都有文件当前位置的输出。 我希望它是这样的:

CLIENT 228 // first we send some data 
SERVER 228 // Server gets it and outputs the same file pos 

CLIENT 228 
CLIENT 456 
SERVER 228 
SERVER 456 

但是我却越来越 - 混淆了我......

SERVER 499384320 
SERVER 499646464 
CLIENT 88604672 
SERVER 499908608 
CLIENT 88866816 
SERVER 500170752 
SERVER 500432896 
SERVER 500695040 
SERVER 500957184 

关于通过接收东西远远更多的消息服务器,而不是客户端发送。它是如何的?从字面上看,看起来客户端只发送80mb的数据,而服务器已经收到500mb的数据......我想,服务器线程应该等待receive(),因为我使用的是阻塞套接字,但这很奇怪。有人可以解释我,为什么我有这个巨大的异步?

回答

3

你假设receive读取整个缓冲区大小一次,但它不一定:

The receive operation may not receive all of the requested number of bytes. Consider using 
the read function if you need to ensure that the requested amount of data is read before the 
blocking operation completes 

receive返回的数据量读取,您应该更改您的代码是这样的:

size_t recvd = mSocket->receive(buffer(buf, bufSize)); 
ofs.write(buf, recvd); 
currPos += recvd; 
相关问题