Simple UDP socket in VC++ MFC
<p>I have been trying to write a working program that takes in data from a UDP socket and displays it in an edit control box as you receive the data (My exposure to c++ is also only about a week :P have only done embedded C code before). I have a working program that can send and output data on a button click but I want something that can do it in real time. The aim is scale this up into a larger GUI program that can send control data to hardware and get responses from them.</p>

<p>I have run into various problems including:</p>

<ol>
<li>The program just not executing my <code>OnReceive</code>function (derived from
<code>CAsyncSocket</code>)</li>
<li>Getting the <code>OnReceive</code> function to run on a separate thread so that it can still run after a button has been clicked sending a control packet to the client then waiting for a response in a while loop</li>
<li>Not being able to output the data in the edit box (tried using both <code>CEdit</code> and <code>CString</code>)</li>
<li><code>ReplaceSel</code> error saying that the type <code>char</code> is incompatible with <code>LPCTSTR</code></li>
</ol>

<p>My code is based on <a href="http://www.codeproject.com/Articles/26715/Simple-client-server-UDP-socket-program" rel="nofollow">this codeproject.com tutorial</a>, being almost exactly what I want but I get the error in 4.</p>

<p>EDIT: the error in 4. disappears when I change it to a <code>TCHAR</code> but then it outputs random chinese characters. The codeproject.com tutorial outputs the correct characters regardless of <code>char</code> or <code>TCHAR</code> declaration. When debugged my code has type <code>wchar_t</code> instead type <code>char</code> like the other code.</p>

<p><a href="http://i.stack.imgur.com/8kBAM.png" rel="nofollow">Chinese output</a></p>

<p>In the working program <code>echoBuffer[0]</code> the character sent and displayed was a <code>1</code></p>

<pre><code>UINT ReceiveData(LPVOID pParam)
{
    CTesterDlg *dlg = (CTesterDlg*)pParam;
    AfxSocketInit(NULL);
    CSocket echoServer;

    // Create socket for sending/receiving datagrams
    if (echoServer.Create(12345, SOCK_DGRAM, NULL) == 0) 
    {
        AfxMessageBox(_T("Create() failed"));
    }

    for (;;) 
    { // Run forever

        // Client address
        SOCKADDR_IN echoClntAddr;

        // Set the size of the in-out parameter
        int clntAddrLen = sizeof(echoClntAddr);

        // Buffer for echo string
        char echoBuffer[ECHOMAX];

        // Block until receive message from a client
        int recvMsgSize = echoServer.ReceiveFrom(echoBuffer, ECHOMAX, (SOCKADDR*)&amp;echoClntAddr, &amp;clntAddrLen, 0);
        if (recvMsgSize &lt; 0)
        {
            AfxMessageBox(_T("RecvFrom() failed"));
        }

        echoBuffer[recvMsgSize] = '\0';

        dlg-&gt;m_edit.ReplaceSel(echoBuffer);
        dlg-&gt;m_edit.ReplaceSel(_T("\r\n"));


    }
}
</code></pre>