UDP发送,接受数据问题!
1.send的程序
struct Head {
int message_len;
int command_num;
int sub_num;
int transact_id;
int serial_num;
} Platform,*pPlatform;
CSocket m_SendSocket;//构造一个套接字对象
//发送数据的初始化
Platform.command_num = 201;
Platform.sub_num = 0;
Platform.serial_num = 0;
Platform.transact_id = 12;
Platform.message_len = sizeof(Platform);
pPlatform=&Platform;
//数据发送
m_SendSocket.Create(2330,SOCK_DGRAM);//创建一个套接字句柄(UDP)
m_SendSocket.SendTo( pPlatform,sizeof(Platform),3550,"192.168.0.16");
receive的程序
Head *pPlatform1;
CSocket m_ReceiveSocket;
char *buff1[256];
CString str1;
//接收数据
m_ReceiveSocket.Create(3550,SOCK_DGRAM);
while(1)
{
if(m_ReceiveSocket.Receive(buff1,256)==SOCKET_ERROR)
{
AfxMessageBox("sockerror");
continue ;
}
else
{
struct Head* pPlatform1 = (struct Head*)buff1;//智能机请求
str1.Format("%d",pPlatform1->command_num);
AfxMessageBox(str1);
}
}
现在的程序可以发送,也可以接受
我想在receive成功接受到数据后,返回给send已经接受到数据
在send写一个接收的程序,接收receive发过来的数据
我直接在receive接到str1后
直接 m_SendSocket.Create(2330,SOCK_DGRAM);//创建一个套接字句柄(UDP)
m_SendSocket.SendTo( str1,sizeof(str1),3550,"192.168.0.16");
程序就运行有错?
为什么呢?该怎么做呢?
问题点数:50、回复次数:9Top
1 楼Caps77(厉兵秣马)回复于 2004-12-01 11:08:55 得分 40
void GetError()
{
LPTSTR lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
MessageBox(NULL,lpMsgBuf,"ERROR",MB_OK);
LocalFree( lpMsgBuf );
}
发送2次数据后
GetError();
同时在接收2次数据后也
GetError()
一下,看看提示什么错误Top
2 楼xeina(亚亚)回复于 2004-12-01 11:09:23 得分 0
我定义了CSocket m_SendSocket;Top
3 楼freeshoot(巴蒂刘)回复于 2004-12-01 11:13:29 得分 10
问题就出在:
m_SendSocket.SendTo( str1,sizeof(str1),3550,"192.168.0.16");
str1是指针否?如果是,那sizeof(str1)就会等于4。Top
4 楼xeina(亚亚)回复于 2004-12-01 11:16:08 得分 0
我把str1换成pPlatform1也不对啊,运行的时候也有错啊Top
5 楼xeina(亚亚)回复于 2004-12-01 11:23:10 得分 0
加了GetError()
第一次是操作成功完成,
第二次是Detected memory leaks!
....
....
....
....
Object dump complete.
The thread 0xA88 has exited with code 3 (0x3).
The program 'E:\ParkClient\ParkClient\Debug\ParkClient.exe' has exited with code 3 (0x3).Top
6 楼xeina(亚亚)回复于 2004-12-01 11:24:30 得分 0
m_SendSocket.Create(2330,SOCK_DGRAM);//创建一个套接字句柄(UDP)
m_SendSocket.SendTo( pPlatform1,sizeof(pPlatform1),3550,"192.168.0.16");
GetError();Top
7 楼freeshoot(巴蒂刘)回复于 2004-12-01 13:44:54 得分 0
m_SendSocket.SendTo(pPlatform1, sizeof(pPlatform1)....)
你想想,前两个参数应该是发送数据指针和发送的数据长度,怎么能这样用?
楼主太粗心。Top
8 楼xeina(亚亚)回复于 2004-12-01 14:06:09 得分 0
我的问题解决了,谢谢各位了!Top
9 楼littleline()回复于 2004-12-01 14:08:20 得分 0
sendto()看msdnTop




