views:

115

answers:

1

Hi,

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();
}
A: 

You should never need to add casts to message map entries.

For an ON_MESSAGE handler, the type of the function must be afx_msg LRESULT (CWnd::*)(WPARAM, LPARAM), so you should change your readData function from this:

LRESULT CUDPClientDlg::readData() {
    ...
}

to this:

LRESULT CUDPClientDlg::readData(WPARAM wParam, LPARAM lParam) {
    ...
}

and remove the cast so that the message map entry becomes:

ON_MESSAGE(WM_SOCKETREAD,readData)
ChrisN
Thanks ChrisN, that did the trick....I was stuck on this for a while, I appreciate your help.Thanks Again!
JB_SO