Home > Back-end >  A few years didn't come, send a question, about the socket communication
A few years didn't come, send a question, about the socket communication

Time:09-18

Didn't come for a long time, account and password forgot, tried half a day to remember,
Ask a question:
Client submit multiple files to the server, the client to select a folder, at all levels within the first upload the file directory, and then in turn within the file all documents sent in the past, the problem is that all documents submitted to the server from time to time to receive, sometimes part received, I ask what conditions lead to appear this kind of alternately in normal and abnormal state, the big ye have this situation?

Simple code (client) :
The lock (& amp; Lock_q_buf);
while (! Q_buf. Empty ()) {
One_nb=q_buf. Front ();
Unlock (& amp; Lock_q_buf);
If (0 x03==one_nb - & gt; CMD [2]) {
Send (conner, one_nb - & gt; CMD, one_nb - & gt; Real_size, 0);
Two_nb=(net_buf *) net_buf_pool: : malloc ();
Two_nb SecureZeroMemory (two_nb, sizeof (*));
The recv (conner, two_nb - & gt; CMD, BUF_SIZE, 0);
Bytes_uploaded=0;
Form4 - & gt; SButton1 - & gt; Enabled=false;
Form4 - & gt; SButton2 - & gt; Enabled=false;
for (i=0; I & lt; Files_to_upload - & gt; The Count. I++) {
Tmp_str1. Operator=
(fold_to_upload + "\ " + files_to_upload - & gt; Operator [] (I));
The file=fopen (tmp_str1 c_str (), "rb");
If (file) {
Fseek (file, 0 l, SEEK_END);
Tmpi1=ftell (file);
Tmpi2=BUF_SIZE - 15 - AnsiString
(files_to_upload - & gt; Operator [] (I)). The Length ();
Fseek (file, 0 l, SEEK_SET);
File_content_order=0;
Do {
Three_nb=(net_buf *) net_buf_pool: : malloc ();
Three_nb SecureZeroMemory (three_nb, sizeof (*));
Three_nb - & gt; CMD [0]=three_nb - & gt; CMD [1]=0 x01;
Three_nb - & gt; CMD [2]=0 x03;
Three_nb - & gt; CMD [3]=0 x03;
MoveMemory (& amp; (three_nb - & gt; CMD [4]), & amp; User_id,
Sizeof (user_id));
MoveMemory (& amp; (three_nb - & gt; CMD [8]), & amp; File_content_order,
Sizeof (file_content_order));
Tmpi0=12;
Tmp_str0. Operator=(files_to_upload - & gt; Operator [] (I));
MoveMemory (& amp; (three_nb - & gt; CMD [tmpi0]), tmp_str0 c_str (),
Tmp_str0. Length ());
Tmpi0 +=tmp_str0. Length ();
Three_nb - & gt; CMD [tmpi0 + +]=0 x1f;
Tmpi3=fread (& amp; (three_nb - & gt; CMD tmpi0), 1, tmpi2, file);
Tmpi0 +=tmpi3;
Three_nb - & gt; CMD=three_nb - [tmpi0] & gt; CMD=0 x17 [tmpi0 + 1];
Three_nb - & gt; Real_size=tmpi0 + 2;
Send (conner, three_nb - & gt; CMD, three_nb - & gt; Real_size, 0);
Net_buf_pool: : free (three_nb);
File_content_order + +;
}
While (tmpi3==tmpi2);
The fclose (file);
}
}
Form4 - & gt; SButton1 - & gt; Enabled=true;
Form4 - & gt; SButton2 - & gt; Enabled=true;
Form4 - & gt; SStatusBar1 - & gt; Panels - & gt; Operator [] (1) - & gt; Text="file upload finished";
}
Net_buf_pool: : free (one_nb);
The lock (& amp; Lock_q_buf);
Q_buf. Pop ();
}
Unlock (& amp; Lock_q_buf);
Simple said this paragraph, traverse files_to_upload various, files_to_upload contains the folder name, opening them to read the content, every time read the file size of BUF_SIZE - 15 - AnsiString (files_to_upload - & gt; Operator [] (I)). The Length (); , in a file used to store data, and then send out,

Server:
Else if (0 x03==one_op0 - & gt; Bufx. Buf [2]) {
If (0 x01==one_op0 - & gt; Bufx. Buf [3]) {
Temp_num0=* ((int *) & amp; (one_op0 - & gt; Bufx. Buf [4]));
Temp_str. Operator=(" ");
Temp_num1=8;
while (! ((0 x17==one_op0 - & gt; Bufx. Buf [temp_num1]) & amp; &
(0 x17==one_op0 - & gt; Bufx. Buf [temp_num1 + 1)))) {
Temp_str. Operator +=(one_op0 - & gt; 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 & lt; Temp_str. Length ()) {
If (' | '.=temp_str. C_str () [temp_num2]);
The 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 & gt;=temp_str. Length ())
Goto FINISHJOURNEY;
Goto NEXT_FOLD;
FINISHJOURNEY:;
REAL_SEND (one_op1 one_op0);
One_op1 - & gt; Bufn [0]=one_op1 - & gt; Bufn [1]=0 x01;
One_op1 - & gt; Bufn [2]=0 x03;
One_op1 - & gt; Bufn [3]=0 x02;
MoveMemory (& amp; (one_op1 - & gt; Bufn [4]), & amp; Temp_num0,
Sizeof (temp_num0));
One_op1 - & gt; Bufn [8]=one_op1 - & gt; Bufn [9]=0 x17;
One_op1 - & gt; Bufx. Len=10;
R_ol_op=WSASend (one_op1 - & gt; S, & amp; (one_op1 - & gt; Bufx), 1,
& Bytes_send, 0, & amp; (one_op1 - & gt; 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 - & gt; Second - & gt; Client_addr)
Closesocket (itrt_bi_map - & gt; Second - & gt;
Client_addr);
Bc_pool: : free ((itrt_bi_map - & gt; Second));
Busi_info. Erase (itrt_bi_map);
}
}
}
}
Else if (0 x03==one_op0 - & gt; Bufx. Buf [3]) {
Temp_num0=* ((int *) & amp; (one_op0 - & gt; 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 *) & amp; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related