UDP Client - version for version not working only in VS 2005
I have a simple UDP client / server program that sends (server) a text string and receives (client) that text string to display in a dialog. It is an MFC C ++ program and I work it correctly in Visual Studio 6.0, Visual Studio 2003 in both debug and release versions. I am trying to get the same code running on Visual Studio 2005 and unfortunately the UDP client only works in debug mode and not in release mode. This is 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 gets the data, and my dialog exits, just exits ..., I commented out the OnOK functions ( ), OnCancel () to see if they are called after receiving the packet, but not in this case.It goes through my whole read function and just exits, it looks like it doesn't return to dialog ....
Again, please keep in mind that I have the same exact code that works in VS 6, VS 2003 in both debug mode and release mode, but I must have this in VS 2005
I have included some code and if anyone can shed some light on what might be happening, I would really welcome it.
BTW, I tried setting the project properties in release mode to turn off optimizations etc. to see if that might cause any problems and still no luck .....
This 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();
}
a source to share
You don't need to add add-ons to your message map entries.
For ON_MESSAGE
, the function type must be afx_msg LRESULT (CWnd::*)(WPARAM, LPARAM)
, so you must change your function readData
from this:
LRESULT CUDPClientDlg::readData() { ... }
:
LRESULT CUDPClientDlg::readData(WPARAM wParam, LPARAM lParam) { ... }
and remove the cast so that the post card entry becomes:
ON_MESSAGE(WM_SOCKETREAD,readData)
a source to share