Well I’m making a Client-Server application and I can send messages to my client just fine but when I do it the other way around (Client to server) the server application just closes down, any help on how to fix this?
public void OnDataReceived(IAsyncResult asyn)
{
try
{
SocketPacket socketData = (SocketPacket)asyn.AsyncState;
int iRx = 0;
iRx = socketData.m_currentSocket.EndReceive(asyn);
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(socketData.dataBuffer,
0, iRx, chars, 0);
System.String szData = new System.String(chars);
area1.AppendText(szData);
WaitForData(socketData.m_currentSocket); // Continue the waiting for data on the Socket
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
After doing some breakpoints I realized it closes after reaching this part when it tries to append it on the textArea it closes without an error.
Any ideas on how to fix this? I guessing something to do with the threads but not sure why it just closes.
Does an exception happen when
AppendTextis called? If yes, can you include the call stack? Is szData valid data when AppendText is called? Try putting a try/catch around the code to get the exception information:One thing that might be going wrong is that you are accessing a UI control from the non-UI thread, but it could be other things. It’s hard to tell from the code snippet you posted.
Updated:
If the exception was that the control is being invoked from the wrong thread, you can try adding a function like this, then calling that instead of accessing the control directly (untested):