几年没来,发个问题,关于socket通信

古典概型 2018-05-23 03:59:35
好久没来,账号和密码都忘了,试了半天才想起来。
提个问题:
客户端向服务器提交多个文件,客户端选中一个文件夹后,首先上传该文件内的各级子目录,然后依次把该文件内的所有文件发送过去。问题是,提交到服务器的文件时而全部接收到,时而部分接收到。请教一下到底什么情况导致出现这种时而正常时而不正常的状态,有没有遇到这种状况的大爷么?

简单贴一下代码(客户端):
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——命令尾


好久没写程序,手生,勿喷
...全文
978 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
古典概型 2018-05-28
  • 打赏
  • 举报
回复
我觉得send和recv返回值的判定不是什么问题,我可能需要做一下echo
CCED136 2018-05-28
  • 打赏
  • 举报
回复
SOCKET通信, 只要服务器端和客户端, recv 和 send 的字节数不出错, 你上面的问题就不会存在
CCED136 2018-05-23
  • 打赏
  • 举报
回复
以下的代码,可能对你有用 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; }
CCED136 2018-05-23
  • 打赏
  • 举报
回复
代码太多了, 没有细看。 主要说一个问题,客户端 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 中的未发送数据。 接收部分同理。
古典概型 2018-05-23
  • 打赏
  • 举报
回复
我怀疑我发送的逻辑有问题,然后用Wireshark抓了一下,发现应该都发过去了。那么文件操作是不是有问题,因为发现服务端产生的文件,有时候一个文件的后门会追加上另一个文件的一部分,那么是不是又有粘包的问题? 归根结底是不是我在服务端的处理太简单了? IOCP本身的用法应该没问题,虽然几年没写程序,然而以前一直用它处理串口操作和文件操作,用法上应该不会出错。

1,316

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧