关于一个数据接收的问题 新手求教

snakebune 2010-07-12 03:06:10
我用socket来接收数据,接收数据部分代码如下:

int reslen=0;
char *buf = new char[100];
char res[4096]="";
CString str;
switch (WSAGETSELECTEVENT(lParam))
{
case FD_READ:
if((reslen=recv(CDST_TestSocket,(char *)&msg,sizeof(msg),0)) == SOCKET_ERROR)
{

list.InsertString(0,"接收数据发生错误。");
return 0L;
}
if ( reslen>0 )
{

memcpy(res, &msg, sizeof(msg) );
for ( int is=0; is<reslen; is++ )
{
TRACE("%x ", res[is]);
}
TRACE("\n");
str.Format("%s",msg.msg);
我要接受的数据较长,由于不能一次接收完所有数据,导致我提取数据部分的代码重复执行,我该怎么解决这个问题。
如果我定义一个全局BUFFER 想把每次接收的res存放进去直到全局BUFFER里的数据位完整数据,再执行数据提取 我该怎样写
...全文
58 点赞 收藏 8
写回复
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
snakebune 2010-07-12
刚才搞错了不好意思 我用的是2楼的方法。。。
回复
snakebune 2010-07-12
我接收到的数据 是用逗号分隔
回复
snakebune 2010-07-12
我这部分的完整代码是这样的
LRESULT CClientTestDlg::OnReadClose(WPARAM wParam,LPARAM lParam)
{

int reslen=0;
char *buf = new char[100];
char res[4096]="";
CString str;
switch (WSAGETSELECTEVENT(lParam))
{
case FD_READ:
if((reslen=recv(CDST_TestSocket,(char *)&msg,sizeof(msg),0)) == SOCKET_ERROR)
{

list.InsertString(0,"接收数据发生错误。");
return 0L;
}
if ( reslen>0 )
{

memcpy(res, &msg, sizeof(msg) );
for ( int is=0; is<reslen; is++ )
{
TRACE("%x ", res[is]);
}
TRACE("\n");
str.Format("%s",msg.msg);

DEV_INFO Dev_Info;
Dev_Info.Data;
DEV_RESULT Dev_Result;
Dev_Result.Data;
DEV_ITEM Dev_Item;
Dev_Item.Index;
DMI_WriteStdHead_Info(VehicleInfo, 1001, "000");
DMI_WriteStdHead_Info(VehicleInfo, 1003, "000");
char data1[100]="";
float testdata1=1234;
sprintf(data1, "%.2f", testdata1);
DMI_WriteStdHead_Result(ResultInfo, 5213,data1,0,TYPE_DATA);
char data2[100]="";
float testdata2=1234;
sprintf(data2, "%.2f", testdata2);
DMI_WriteStdHead_Result(ResultInfo, 5215,data2,0,TYPE_DATA);
char data3[100]="";
float testdata3=1234;
sprintf(data3, "%.2f", testdata3);
DMI_WriteStdHead_Result(ResultInfo, 7023,data3,0,TYPE_DATA);
char data4[100]="";
float testdata4=1223;
sprintf(data4, "%.2f", testdata4);
DMI_WriteStdHead_Result(ResultInfo, 9077,data4,0,TYPE_DATA);
char data5[100]="";
float testdata5=33;
sprintf(data5, "%.2f", testdata5);
DMI_WriteStdHead_Result(ResultInfo, 7027,data5,0,TYPE_DATA);
char data6[100]="";
float testdata6=123.4;
sprintf(data6, "%.2f", testdata6);
DMI_WriteStdHead_Result(ResultInfo, 6207,data6,0,TYPE_DATA);
char data7[100]="";
float testdata7=123.4;
sprintf(data7, "%.2f", testdata7);
DMI_WriteStdHead_Result(ResultInfo, 6208,data7,0,TYPE_DATA);
char data8[100]="";
float testdata8=123.4;
sprintf(data8, "%.2f", testdata8);
DMI_WriteStdHead_Result(ResultInfo, 5210,data8,0,TYPE_DATA);
char data9[100]="";
float testdata9=123.4;
sprintf(data9, "%.2f", testdata9);
DMI_WriteStdHead_Result(ResultInfo, 6209,data9,0,TYPE_DATA);
char data10[100]="";
float testdata10=1234.5;
sprintf(data10, "%.2f", testdata10);
DMI_WriteStdHead_Result(ResultInfo, 5216,data10,0,TYPE_DATA);
char data11[100]="";
float testdata11=23;
sprintf(data11, "%.2f", testdata11);
DMI_WriteStdHead_Result(ResultInfo, 5213,data11,0,TYPE_DATA);
char data12[100]="";
float testdata12=123.4;
sprintf(data12, "%.2f", testdata12);
DMI_WriteStdHead_Result(ResultInfo, 5211,data12,0,TYPE_DATA);
char data13[100]="";
float testdata13=123.4;
sprintf(data13, "%.2f", testdata13);
DMI_WriteStdHead_Result(ResultInfo, 6210,data13,0,TYPE_DATA);
char data14[100]="";
float testdata14=1234.5;
sprintf(data14, "%.2f", testdata14);
DMI_WriteStdHead_Result(ResultInfo, 5217,data14,0,TYPE_DATA);
char data15[100]="";
float testdata15=23;
sprintf(data15, "%.2f", testdata15);
DMI_WriteStdHead_Result(ResultInfo, 5214,data15,0,TYPE_DATA);
char data16[100]="";
float testdata16=123.4;
sprintf(data16, "%.2f", testdata16);
DMI_WriteStdHead_Result(ResultInfo, 5212,data16,0,TYPE_DATA);
char data17[100]="";
float testdata17=123.4;
sprintf(data17, "%.2f", testdata17);
DMI_WriteStdHead_Result(ResultInfo, 6211,data17,0,TYPE_DATA);
char data18[100]="";
float testdata18=1234.5;
sprintf(data18, "%.2f", testdata18);
DMI_WriteStdHead_Result(ResultInfo, 5218,data18,0,TYPE_DATA);
char data19[100]="";
float testdata19=23;
sprintf(data19, "%.2f", testdata19);
DMI_WriteStdHead_Result(ResultInfo, 5215,data19,0,TYPE_DATA);
char data20[100]="";
float testdata20=1000;
sprintf(data20, "%.2f", testdata20);
DMI_WriteStdHead_Result(ResultInfoMAX, 6200,data20,0,TYPE_DATA);
char data21[100]="";
float testdata21=1000;
sprintf(data21, "%.2f", testdata21);
DMI_WriteStdHead_Result(ResultInfoMIN, 6200,data20,0,TYPE_DATA);
char data22[100]="";
float testdata22=1000;
sprintf(data22, "%.2f", testdata22);
DMI_WriteStdHead_Result(ResultInfoMAX, 6202,data22,0,TYPE_DATA);
char data23[100]="";
float testdata23=1000;
sprintf(data23, "%.2f", testdata23);
DMI_WriteStdHead_Result(ResultInfoMIN, 6202,data23,0,TYPE_DATA);
char data24[100]="";
float testdata24=1000;
sprintf(data24, "%.2f", testdata24);
DMI_WriteStdHead_Result(ResultInfoMAX, 6204,data24,0,TYPE_DATA);
char data25[100]="";
float testdata25=1000;
sprintf(data25, "%.2f", testdata25);
DMI_WriteStdHead_Result(ResultInfoMIN, 6204,data25,0,TYPE_DATA);
char data26[100]="";
float testdata26=25.6;
sprintf(data26, "%.2f", testdata26);
DMI_WriteStdHead_Result(ResultInfo, 6206,data26,0,TYPE_DATA);
char data27[100]="";
float testdata27=45;
sprintf(data27, "%.2f", testdata27);
DMI_WriteStdHead_Result(ResultInfo, 5012,data27,0,TYPE_DATA);
char data28[100]="";
float testdata28=49;
sprintf(data28, "%.2f", testdata28);
DMI_WriteStdHead_Result(ResultInfo, 5013,data28,0,TYPE_DATA);
char data29[100]="";
float testdata29=41;
sprintf(data29, "%.2f", testdata29);
DMI_WriteStdHead_Result(ResultInfo, 5014,data29,0,TYPE_DATA);
char data30[100]="";
float testdata30=45;
sprintf(data30, "%.2f", testdata30);
DMI_WriteStdHead_Result(ResultInfo, 5015,data30,0,TYPE_DATA);
char data[100]="";
float testdata=1234;
sprintf(data, "%.2f", testdata);
DMI_WriteStdHead_Result(ResultInfo, 7008,data,0,TYPE_DATA);
DMI_WriteStdHead_Item(ItemInfo, 244);
DMI_WriteStdHead_Item(ItemInfo, 214);
DMI_WriteStdHead_Item(ItemInfo, 251);
DMI_WriteStdHead_Item(ItemInfo, 204);




char st[200]="";


int pos=m_ClistCtrl1.InsertItem(m_ClistCtrl1.GetItemCount(),res);
从这开始
DMI_GetField(res,st,0);
m_ClistCtrl1.SetItemText(pos,0,st);
DMI_GetField(res,st,1);
m_ClistCtrl1.SetItemText(pos,1,st);
DMI_GetField(res,st,2);
m_ClistCtrl1.SetItemText(pos,2,st);
DMI_GetField(res,st,3);
m_ClistCtrl1.SetItemText(pos,3,st);
DMI_GetField(res,st,4);
m_ClistCtrl1.SetItemText(pos,4,st);
DMI_GetField(res,st,5);
m_ClistCtrl1.SetItemText(pos,5,st);
DMI_GetField(res,st,6);
m_ClistCtrl1.SetItemText(pos,6,st);
DMI_GetField(res,st,7);
m_ClistCtrl1.SetItemText(pos,7,st);
DMI_GetField(res,st,8);
m_ClistCtrl1.SetItemText(pos,8,st);
DMI_GetField(res,st,9);
m_ClistCtrl1.SetItemText(pos,9,st);
DMI_GetField(res,st,10);
m_ClistCtrl1.SetItemText(pos,10,st);
DMI_GetField(res,st,11);
m_ClistCtrl1.SetItemText(pos,11,st);
DMI_GetField(res,st,12);
m_ClistCtrl1.SetItemText(pos,12,st);
DMI_GetField(res,st,13);
m_ClistCtrl1.SetItemText(pos,13,st);
DMI_GetField(res,st,14);
m_ClistCtrl1.SetItemText(pos,14,st);
DMI_GetField(res,st,15);
m_ClistCtrl1.SetItemText(pos,15,st);
DMI_GetField(res,st,16);
m_ClistCtrl1.SetItemText(pos,16,st);
DMI_GetField(res,st,17);
m_ClistCtrl1.SetItemText(pos,17,st);
DMI_GetField(res,st,18);
m_ClistCtrl1.SetItemText(pos,18,st);
DMI_GetField(res,st,19);
m_ClistCtrl1.SetItemText(pos,19,st);
DMI_GetField(res,st,20);
m_ClistCtrl1.SetItemText(pos,20,st);
DMI_GetField(res,st,21);
m_ClistCtrl1.SetItemText(pos,21,st);
到这行 每段代码执行了3次 下面的不能读取数据


DMI_GetField(res,st,22);
m_ClistCtrl1.SetItemText(pos,22,st);
DMI_GetField(res,st,23);
m_ClistCtrl1.SetItemText(pos,23,st);
DMI_GetField(res,st,24);
m_ClistCtrl1.SetItemText(pos,24,st);
DMI_GetField(res,st,25);
m_ClistCtrl1.SetItemText(pos,25,st);
DMI_GetField(res,st,26);
m_ClistCtrl1.SetItemText(pos,26,st);
DMI_GetField(res,st,27);
m_ClistCtrl1.SetItemText(pos,27,st);
DMI_GetField(res,st,28);
m_ClistCtrl1.SetItemText(pos,28,st);
DMI_GetField(res,st,29);
m_ClistCtrl1.SetItemText(pos,29,st);
DMI_GetField(res,st,30);
m_ClistCtrl1.SetItemText(pos,30,st);
DMI_GetField(res,st,31);
m_ClistCtrl1.SetItemText(pos,31,st);
DMI_GetField(res,st,32);
m_ClistCtrl1.SetItemText(pos,32,st);
DMI_GetField(res,st,33);
m_ClistCtrl1.SetItemText(pos,33,st);
DMI_GetField(res,st,34);
m_ClistCtrl1.SetItemText(pos,34,st);
DMI_GetField(res,st,35);
m_ClistCtrl1.SetItemText(pos,35,st);
DMI_GetField(res,st,36);
m_ClistCtrl1.SetItemText(pos,36,st);



}
break;
case FD_CLOSE:
closesocket(CDST_TestSocket);
list.InsertString(0,"服务器退出。");
IsTrue = FALSE;
break;
}
return 0L;
}
麻烦大家帮忙看下
回复
kemee 2010-07-12
[Quote=引用 4 楼 snakebune 的回复:]

3楼的朋友能不能 具体说明下 我根据你的方法解决了重复读数的问题 但是我所读取的数据不完整 只有一部分
[/Quote]

不对啊 我用的CSocket阻塞式的,就3楼贴的那几句传的,4M大小的图片完全没问题啊,当然我是本机测试,没有试过网络状态不好的情况
回复
snakebune 2010-07-12
3楼的朋友能不能 具体说明下 我根据你的方法解决了重复读数的问题 但是我所读取的数据不完整 只有一部分
回复
kemee 2010-07-12
我刚遇到过这问题。。。大文件一开始传过去都没内容,图片就显示半张,楼主问题简单就这样搞:
发送方


file.Open();
while(1)
{
int len = myFile.Read(temp, 1024);
if(len == 0)
break;

sockRecv.Send(temp, len);
}
myFile.Close();


接收方一样处理:


file.Open();
while(1)
{
int len = sockClient.Receive(buffer, 1024);
if(len == 0)
break;
file.Write(buffer, len);
}
file.Close();

回复
Eleven 2010-07-12
循环接收,类似下面这样的
int bufflen = 1024; //比如总的数据大小是1024字节
char buf[1024] = {0};
int leave = 1024; //
int nRet = 0;
int nCount = 0;
while(leave>0)
{
nRet = recv(CDST_TestSocket, &buf[nCount], leave, 0);
nCount += nRet;
leave -= nRet;
}
回复
cpp2017 2010-07-12
定义一个大的buffer长度要足够

reslen=recv ....

然后

memcpy(buffer+wCurSize,临时buffer,reslen);

wCurSize+= reslen

//如果检测到结束
wCurSize=0;
清空buffer,

回复
发动态
发帖子
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
社区公告
暂无公告