好久沒來,賬號和密碼都忘了,試了半天才想起來。
提個問題:
客戶端向服務器提交多個檔案,客戶端選中一個檔案夾后,首先上傳該檔案內的各級子目錄,然后依次把該檔案內的所有檔案發送過去。問題是,提交到服務器的檔案時而全部接收到,時而部分接收到。請教一下到底什么情況導致出現這種時而正常時而不正常的狀態,有沒有遇到這種狀況的大爺么?
簡單貼一下代碼(客戶端):
lock(&lock_q_buf);
while (!q_buf.empty()) {
one_nb = q_buf.front();
unlock(&lock_q_buf);
if (0x03 == one_nb->cmd[2]) {
send(conner, one_nb->cmd, one_nb->real_size, 0);
two_nb = (net_buf*)net_buf_pool::malloc();
SecureZeroMemory(two_nb, sizeof(*two_nb));
recv(conner, two_nb->cmd, BUF_SIZE, 0);
bytes_uploaded = 0;
Form4->sButton1->Enabled = false;
Form4->sButton2->Enabled = false;
for (i = 0; i < files_to_upload->Count; i++) {
tmp_str1.operator =
(fold_to_upload + "\\" + files_to_upload->operator[](i));
file = fopen(tmp_str1.c_str(), "rb");
if (file) {
fseek(file, 0L, SEEK_END);
tmpi1 = ftell(file);
tmpi2 = BUF_SIZE - 15 - AnsiString
(files_to_upload->operator[](i)).Length();
fseek(file, 0L, SEEK_SET);
file_content_order = 0;
do {
three_nb = (net_buf*)net_buf_pool::malloc();
SecureZeroMemory(three_nb, sizeof(*three_nb));
three_nb->cmd[0] = three_nb->cmd[1] = 0x01;
three_nb->cmd[2] = 0x03;
three_nb->cmd[3] = 0x03;
MoveMemory(&(three_nb->cmd[4]), &user_id,
sizeof(user_id));
MoveMemory(&(three_nb->cmd[8]), &file_content_order,
sizeof(file_content_order));
tmpi0 = 12;
tmp_str0.operator = (files_to_upload->operator[](i));
MoveMemory(&(three_nb->cmd[tmpi0]), tmp_str0.c_str(),
tmp_str0.Length());
tmpi0 += tmp_str0.Length();
three_nb->cmd[tmpi0++] = 0x1f;
tmpi3 = fread(&(three_nb->cmd[tmpi0]), 1, tmpi2, file);
tmpi0 += tmpi3;
three_nb->cmd[tmpi0] = three_nb->cmd[tmpi0 + 1] = 0x17;
three_nb->real_size = tmpi0 + 2;
send(conner, three_nb->cmd, three_nb->real_size, 0);
net_buf_pool::free(three_nb);
file_content_order++;
}
while (tmpi3 == tmpi2);
fclose(file);
}
}
Form4->sButton1->Enabled = true;
Form4->sButton2->Enabled = true;
Form4->sStatusBar1->Panels->operator [](1)->Text = "檔案上傳完畢";
}
net_buf_pool::free(one_nb);
lock(&lock_q_buf);
q_buf.pop();
}
unlock(&lock_q_buf);
簡單說一下這段,遍歷files_to_upload各項,files_to_upload包含了檔案夾內的檔案名,依次打開它們讀取內容,每次讀取檔案的大小為BUF_SIZE - 15 - AnsiString(files_to_upload->operator[](i)).Length();,用來保存檔案內資料,然后發送出去。
服務器:
else if (0x03 == one_op0->bufx.buf[2]) {
if (0x01 == one_op0->bufx.buf[3]) {
temp_num0 = *((int *)&(one_op0->bufx.buf[4]));
temp_str.operator = ("");
temp_num1 = 8;
while (!((0x17 == one_op0->bufx.buf[temp_num1]) &&
(0x17 == one_op0->bufx.buf[temp_num1 + 1]))) {
temp_str.operator += (one_op0->bufx.buf[temp_num1]);
temp_num1++;
}
temp_str0.operator = (busi_root_dir + "\\SLZY\\EXAMS\\");
temp_str0.operator += (IntToStr((int)temp_num0));
if (!DirectoryExists(temp_str0))
ForceDirectories(temp_str0);
temp_num2 = 0;
temp_num1 = 0;
NEXT_FOLD:
while (temp_num2 < temp_str.Length()) {
if ('|' != temp_str.c_str()[temp_num2]);
else
break;
temp_num2++;
}
temp_str1.operator = (temp_str0 + "\\");
temp_str1.operator +=
(temp_str.SubString(temp_num1 + 1,
temp_num2 - temp_num1));
temp_num2 += 1;
temp_num1 = temp_num2;
if (!DirectoryExists(temp_str1))
ForceDirectories(temp_str1);
if (temp_num2 >= temp_str.Length())
goto FINISHJOURNEY;
goto NEXT_FOLD;
FINISHJOURNEY: ;
REAL_SEND(one_op1, one_op0);
one_op1->bufn[0] = one_op1->bufn[1] = 0x01;
one_op1->bufn[2] = 0x03;
one_op1->bufn[3] = 0x02;
MoveMemory(&(one_op1->bufn[4]), &temp_num0,
sizeof(temp_num0));
one_op1->bufn[8] = one_op1->bufn[9] = 0x17;
one_op1->bufx.len = 10;
r_ol_op = WSASend(one_op1->s, &(one_op1->bufx), 1,
&bytes_send, 0, &(one_op1->wsol), 0);
if (r_ol_op) {
if ((WSA_IO_PENDING != (ec = WSAGetLastError()))) {
oos_pool::free(one_op1);
itrt_bi_map = busi_info.find((temp_num0));
if (busi_info.end() != itrt_bi_map) {
if (INVALID_SOCKET !=
itrt_bi_map->second->client_addr)
closesocket(itrt_bi_map->second->
client_addr);
bc_pool::free((itrt_bi_map->second));
busi_info.erase(itrt_bi_map);
}
}
}
}
else if (0x03 == one_op0->bufx.buf[3]) {
temp_num0 = *((int *)&(one_op0->bufx.buf[4]));
temp_str.operator = (busi_root_dir + "\\SLZY\\EXAMS\\");
temp_str.operator += (IntToStr((int)temp_num0));
temp_str.operator += ("\\");
temp_str0.operator = ("");
temp_num1 = *((int *)&(one_op0->bufx.buf[8]));
temp_num2 = 12;
while (((0x1f != one_op0->bufx.buf[temp_num2]))) {
temp_str0.operator += (one_op0->bufx.buf[temp_num2]);
temp_num2++;
}
temp_str1.operator = (temp_str.operator +(temp_str0));
if (temp_num1);
else {
if (FileExists(temp_str1))
DeleteFile(temp_str1);
}
if(one_file.is_open())one_file.close();
one_file.open(temp_str1.c_str(),
std::fstream::binary | std::fstream::app);
if (one_file.good()) {
temp_num2++;
one_file.write(&(one_op0->bufx.buf[temp_num2]),
one_op0->bufx.len - 15 - temp_str0.Length());
one_file.close();
}
}
簡單描述一下,IOCP模型,帶有接收標記的包直接扔到一個佇列里不管,由專門執行緒處理。0x01 == one_op0->bufx.buf[3]表示收到目錄,決議包,然后在目標檔案夾下創建目錄。0x03 == one_op0->bufx.buf[3]表示收到檔案資料,決議,獲取包內的檔案名,打開檔案,然后one_file.write(&(one_op0->bufx.buf[temp_num2]), one_op0->bufx.len - 15 - temp_str0.Length());將資料寫進去追加進去。
另外包結構為:
0x010x01——命令頭
0x03——試卷上傳
0x01——目錄結構上傳
工號——整型
目錄結構——ASCII字串
0x170x17——命令尾
0x010x01——命令頭
0x03——試卷上傳
0x03——上傳試卷內容
工號——整型
檔案內容序號——整型
檔案名——ASCII字串
0x1f
檔案內資料——二進制
0x170x17——命令尾
好久沒寫程式,手生,勿噴
uj5u.com熱心網友回復:
我懷疑我發送的邏輯有問題,然后用Wireshark抓了一下,發現應該都發過去了。那么檔案操作是不是有問題,因為發現服務端產生的檔案,有時候一個檔案的后門會追加上另一個檔案的一部分,那么是不是又有粘包的問題?歸根結底是不是我在服務端的處理太簡單了?
IOCP本身的用法應該沒問題,雖然幾年沒寫程式,然而以前一直用它處理串口操作和檔案操作,用法上應該不會出錯。
uj5u.com熱心網友回復:
代碼太多了, 沒有細看。 主要說一個問題,客戶端 send 函式 及 recv 函式, 必須判斷回傳值。 你要知道, 你期望發送或接收的資料,很大的可能,一個 send/recv 搞不定,這時就需要自己處理了。 簡單舉例, int sentbytes = send(conner, one_nb->cmd, one_nb->real_size, 0); 其中 one_nb->real_size = 102400。 這條陳述句執行后, 很可能 sentbytes 小于 102400, 這意味著 one_nb->cmd 中的資料沒有被發送完整。 此時, 你應該繼續 send one_nb->cmd 中的未發送資料。 接收部分同理。uj5u.com熱心網友回復:
以下的代碼,可能對你有用bool __fastcall TWIN32Socket::Send(LPCVOID Buffer, INT Length)
{
LPCSTR DataBuffer = (LPCSTR)(Buffer);
INT DataLength = Length;
while ( DataLength > 0 ) {
Length = ::send(FSocketHandle, DataBuffer, DataLength, 0);
if ( Length >= DataLength ) { return true; }
if ( Length <= 0 ) { return false; }
DataBuffer += Length;
DataLength -= Length;
}
return false;
}
bool __fastcall TWIN32Socket::Recv(LPVOID Buffer, INT Length)
{
LPSTR DataBuffer = (LPSTR)(Buffer);
INT DataLength = Length;
while ( DataLength > 0 ) {
Length = ::recv(FSocketHandle, DataBuffer, DataLength, 0);
if ( Length >= DataLength ) { return true; }
if ( Length <= 0 ) { return false; }
DataBuffer += Length;
DataLength -= Length;
}
return false;
}
uj5u.com熱心網友回復:
我覺得send和recv回傳值的判定不是什么問題,我可能需要做一下echouj5u.com熱心網友回復:
SOCKET通信, 只要服務器端和客戶端, recv 和 send 的位元組數不出錯, 你上面的問題就不會存在轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/47619.html
標籤:網絡及通訊開發
