UDP 客户端 - 发布版本仅在 VS 2005 中不起作用
我有一个简单的 UDP 客户端/服务器程序,它发送(服务器)文本字符串并接收(客户端)该文本字符串以显示在对话框上。 这是一个 MFC C++ 程序,我让它在 Visual Studio 6.0、Visual Studio 2003 的调试和发布版本中正常工作。 我试图在 Visual Studio 2005 上执行相同的代码,不幸的是 UDP 客户端似乎只能在调试模式下工作,而不能在发布模式下工作。 如果我尝试在发布模式下运行 UDP 客户端可执行文件,就会发生以下情况:当 UDP 客户端从服务器接收到数据包时,我的读取函数被调用,它接收数据并且我的对话框退出,只是退出...... 。 我注释掉了 OnOK()、OnCancel() 函数,以查看它们是否在收到数据包后被调用,但事实并非如此。 它完成了我的整个读取功能并退出,就像它没有返回到对话框一样......
再次,请记住我在 VS 6、VS 2003 的调试和发布模式下都有相同的代码工作,但我必须在 VS 2005 中使用这个
我已经包含了一些代码,如果有人能够阐明可能发生的情况,我将非常感激。
顺便说一下,我已经尝试过设置发布模式下的项目属性禁用优化等,看看这是否会导致任何问题,但仍然没有运气......
这是我的 UDP 客户端应用程序的实现文件中的内容:
BEGIN_MESSAGE_MAP(CUDPClientDlg, CDialog)
ON_MESSAGE(WM_SOCKETREAD,(LRESULT(AFX_MSG_CALL CWnd::*)(WPARAM, LPARAM))readData)
END_MESSAGE_MAP()
BOOL CUDPClientDlg::OnInitDialog()
{
// Socket Initialization
WSADATA data;
if (WSAStartup(MAKEWORD(2,2), &data) != 0) return(0);
int ret;
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (!sock)
{
WSACleanup();
return(0);
}
saServer.sin_family = AF_INET;
saServer.sin_addr.s_addr = INADDR_ANY;
saServer.sin_port = htons(0x1983);
et = bind(sock, (SOCKADDR *)&saServer, sizeof(SOCKADDR));
WSAAsyncSelect(sock, this->m_hWnd, WM_SOCKETREAD, FD_READ);
}
LRESULT CUDPClientDlg::readData()
{
char bufferTMP[4096];
memset(bufferTMP, '\0', sizeof(bufferTMP));
socklen_t fromaddrLen = sizeof(fromSockAddr);
recvfrom(sock, bufferTMP, sizeof(bufferTMP)-1, 0, (struct sockaddr*)
&fromSockAddr, &fromaddrLen);
SetDlgItemText(IDC_EDIT1, bufferTMP);
return 1;
}
void CUDPClientDlg::OnExit()
{
closesocket(sock);
WSACleanup();
OnOK();
}
I have a simple UDP client/server program that that sends (server) a text string and receives (client) that text string to display on the dialog box. This is a MFC C++ program and I have it working properly in Visual Studio 6.0, Visual Studio 2003 in both the debug and release versions. I am trying to get the same code executing on Visual Studio 2005 and unfortunately the UDP client only seems to work in the debug mode, and not in release mode. Here is exactly what happens if I try to run the UDP client executable in release mode: When the UDP client receives a packet from the server, my read function gets called, and it receives the data and my dialog exits, just exits....
I commented out my OnOK(), OnCancel() functions to see if they were being called after it receives the packet, but not the case. It gets through my entire read function and just exits, it's like it's not coming back to the dialog....
Again, please keep in mind I have the same exact code working in VS 6, VS 2003 in both debug and release mode, but I have to have this in VS 2005
I've included some code and if anyone can shed any light on what may be happening, I would greatly apperciate it.
By the way, I've tried setting the project properties under release mode to disable optimization, etc to see if that could be causing any problems and still no luck.....
Here is what I have in my implementation file for my UDP client application:
BEGIN_MESSAGE_MAP(CUDPClientDlg, CDialog)
ON_MESSAGE(WM_SOCKETREAD,(LRESULT(AFX_MSG_CALL CWnd::*)(WPARAM, LPARAM))readData)
END_MESSAGE_MAP()
BOOL CUDPClientDlg::OnInitDialog()
{
// Socket Initialization
WSADATA data;
if (WSAStartup(MAKEWORD(2,2), &data) != 0) return(0);
int ret;
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (!sock)
{
WSACleanup();
return(0);
}
saServer.sin_family = AF_INET;
saServer.sin_addr.s_addr = INADDR_ANY;
saServer.sin_port = htons(0x1983);
et = bind(sock, (SOCKADDR *)&saServer, sizeof(SOCKADDR));
WSAAsyncSelect(sock, this->m_hWnd, WM_SOCKETREAD, FD_READ);
}
LRESULT CUDPClientDlg::readData()
{
char bufferTMP[4096];
memset(bufferTMP, '\0', sizeof(bufferTMP));
socklen_t fromaddrLen = sizeof(fromSockAddr);
recvfrom(sock, bufferTMP, sizeof(bufferTMP)-1, 0, (struct sockaddr*)
&fromSockAddr, &fromaddrLen);
SetDlgItemText(IDC_EDIT1, bufferTMP);
return 1;
}
void CUDPClientDlg::OnExit()
{
closesocket(sock);
WSACleanup();
OnOK();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Windows 防火墙也可能导致发布版本无法正常工作。
A Release version not working can also be caused by Windows Firewall.
您永远不需要向消息映射条目添加强制转换。
对于
ON_MESSAGE
处理程序,类型函数的 必须是 afx_msg LRESULT (CWnd::*)(WPARAM, LPARAM) ,因此您应该将readData
函数从 this: 更改为 this:
并删除强制转换这样消息映射条目就变成:
You should never need to add casts to message map entries.
For an
ON_MESSAGE
handler, the type of the function must beafx_msg LRESULT (CWnd::*)(WPARAM, LPARAM)
, so you should change yourreadData
function from this:to this:
and remove the cast so that the message map entry becomes: