客户端代码
struct OpenPoint
{
short Game_Function;
char UserName[20];
int mOpenPoint;
};
CSocket m_pSocket;
OpenPoint m_OpenPoint;
char* CharUserName="daf"
ZeroMemory(&m_OpenPoint,sizeof(OpenPoint));
m_OpenPoint.Game_Function=2;
strcpy(m_OpenPoint.UserName,CharUserName);
m_OpenPoint.UserName[strlen(CharUserName)]='\0';
m_OpenPoint.mOpenPoint=100;
int send = 0;
while(send<sizeof(OpenPoint))
{
int nCharSend=m_pSocket->Send((void*)(&m_OpenPoint+send),sizeof(OpenPoint)-send,0);
send+=nCharSend;
}
服务器代码 struct OpenPoint
{
char UserName[20];
int mOpenPoint;
};
CSocket m_pServerSocket;
OpenPoint m_OpenPoint;
short temp;
m_pServerSocket->Receive(&temp,sizeof(short),0);
switch(temp)
{
case 0:
{
break;
} case 2:
{
ZeroMemory(&m_OpenPoint,sizeof(OpenPoint));
int receive = 0;
while(receive<sizeof(OpenPoint))
{
int nCharSend=m_pServerSocket->Receive((void*)(&m_OpenPoint+receive),sizeof(OpenPoint)-receive,0);
receive+=nCharSend;
}
} 为什么接收到的m_pServerSocket.mOpenPoint=6553600(本来应该是100)而m_pServerSocket.UserName则正常?
struct OpenPoint
{
short Game_Function;
char UserName[20];
int mOpenPoint;
};
CSocket m_pSocket;
OpenPoint m_OpenPoint;
char* CharUserName="daf"
ZeroMemory(&m_OpenPoint,sizeof(OpenPoint));
m_OpenPoint.Game_Function=2;
strcpy(m_OpenPoint.UserName,CharUserName);
m_OpenPoint.UserName[strlen(CharUserName)]='\0';
m_OpenPoint.mOpenPoint=100;
int send = 0;
while(send<sizeof(OpenPoint))
{
int nCharSend=m_pSocket->Send((void*)(&m_OpenPoint+send),sizeof(OpenPoint)-send,0);
send+=nCharSend;
}
服务器代码 struct OpenPoint
{
char UserName[20];
int mOpenPoint;
};
CSocket m_pServerSocket;
OpenPoint m_OpenPoint;
short temp;
m_pServerSocket->Receive(&temp,sizeof(short),0);
switch(temp)
{
case 0:
{
break;
} case 2:
{
ZeroMemory(&m_OpenPoint,sizeof(OpenPoint));
int receive = 0;
while(receive<sizeof(OpenPoint))
{
int nCharSend=m_pServerSocket->Receive((void*)(&m_OpenPoint+receive),sizeof(OpenPoint)-receive,0);
receive+=nCharSend;
}
} 为什么接收到的m_pServerSocket.mOpenPoint=6553600(本来应该是100)而m_pServerSocket.UserName则正常?
解决方案 »
- VC的工程,除了debug/release以外,还可以建立其他类型的工程吗
- 哪位使用过hu不变矩对图像的轮廓进行过比较?能不能给个例子?多谢,或者请讲流程
- vc/MFC 高手进来一下下哈!!我是一只才雪它的菜鸟!!拜托了!!
- 新建工程默认生成的应用程序界面不是最大化,怎么样让界面打开时就是全屏,不要最大化按钮?
- Dialog中的CStatic加载了背景图以后,界面按纽不可用了,为什么?
- 寻求帮助,一个调用dll函数的问题
- 多个文件包含同一个头文件资源重定义问题
- 救救我!
- 使用TADOConnection的dll需要delphi环境支持吗?
- 哪里可以下载Charles Petzold 的 Programming Windows 95
- 请帮忙看一下
- 为什么读取文件会出错?
关于这一点可以用sizeof(OpenPoint)来确认, 返回值不是你期望的!
不同的机器对于对齐属性有不同的设置,所以不进行1byte对齐而在不同的机器间传递结构一定会有多余的填充数据扰乱传输结果...关于控制结构(struct)对齐属性有多种方法,最简单的方法如下:1)open menu "project\settings..."
2)select "C\C++" tab
3)select "Code generation" category
4)select "1 Byte" from "struct member alignment" list
5)rebuild all projects and run again.
{
short Game_Function; //和USERNAME前2字节共占4字节
char UserName[20]; //剩下18字节,占5个四字节
int mOpenPoint; //占一个4字节.
};
struct OpenPoint
{
char UserName[20]; //占5个四字节
int mOpenPoint; //占一个四字节
};解决方法:short Game_Function改为int类型就 可以 了.