用socket发送结构体 却只收到其中一个数据?
结构体:structINFO{intflag;intnumber;inttest;};客户端发送核心代码:info.flag=11;info.number=112;inf...
结构体:
struct INFO
{
int flag;
int number;
int test;
};
客户端发送核心代码:
info.flag=11;
info.number=112;
info.test=113;
memcpy(SendBuffer,&info,sizeof(info));
Ret = send(CientSocket, SendBuffer, (int)strlen(SendBuffer), 0);
服务器接收端核心代码:
memset(RecvBuffer, 0x00, sizeof(RecvBuffer));
Ret = recv(CientSocket, RecvBuffer, MAX_PATH, 0);
if ( Ret == 0 || Ret == SOCKET_ERROR )
{
cout<<"client out"<<endl;
break;
}
memcpy(&info,RecvBuffer,sizeof(info));
cout<<"info.flag:"<<info.flag<<" info.number:"<<info.number<<" info.test:"<<info.test<<endl;
结果:
flag=11,number=0,test=0
也就是无论结构体有多少int类型都只是能够正确收到第一个值,其他都是0.求解啊! 展开
struct INFO
{
int flag;
int number;
int test;
};
客户端发送核心代码:
info.flag=11;
info.number=112;
info.test=113;
memcpy(SendBuffer,&info,sizeof(info));
Ret = send(CientSocket, SendBuffer, (int)strlen(SendBuffer), 0);
服务器接收端核心代码:
memset(RecvBuffer, 0x00, sizeof(RecvBuffer));
Ret = recv(CientSocket, RecvBuffer, MAX_PATH, 0);
if ( Ret == 0 || Ret == SOCKET_ERROR )
{
cout<<"client out"<<endl;
break;
}
memcpy(&info,RecvBuffer,sizeof(info));
cout<<"info.flag:"<<info.flag<<" info.number:"<<info.number<<" info.test:"<<info.test<<endl;
结果:
flag=11,number=0,test=0
也就是无论结构体有多少int类型都只是能够正确收到第一个值,其他都是0.求解啊! 展开
展开全部
这个问题在于Ret = send(CientSocket, SendBuffer, (int)strlen(SendBuffer), 0);
改为:Ret = send(CientSocket, SendBuffer, sizeof(info), 0);
改为:Ret = send(CientSocket, SendBuffer, sizeof(info), 0);
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询