Tamas Honfi

Tamas Honfi

  • NA
  • 2
  • 0

c# socket read problem

Feb 9 2006 7:21 AM
Hi! I wrote a TCP Streaming socket server application :), what handles clients (connecting, disconnecting, sending and receiving data).
It works fine, until I want to send a large amount of data through the socket. In that case, i don't know why, socket.read doesn't reads all part of the incoming message... (???) After some loops it works as if wasn't any data on the socket available for reading. It blocks. And socket.Poll as well I use for see if the client is connected, or not.
I wrote that again width asynchronus functions as well instead of threading, but got the same anomaly. I could always reproduce it, width VS 2003, and 2005 as well.
(I use VS 2005 Express)
Unfortunately I've no more ideas... Can somebody help me here?
The source of the receiving function is:

private void WaitMessages()
{
int bytesRead;
StringBuilder sb = new StringBuilder();

string content = string.Empty,
msg = string.Empty; while (m_running)
{
try
{
if (m_socket.Poll(-1, SelectMode.SelectRead))
{
sb.Remove(0, sb.Length);
bytesRead = m_socket.Receive(m_buffer);
sb.Append(
Encoding.UTF8.GetString(m_buffer)
);

content = sb.ToString();
msg += content;

if (bytesRead == 0 || content == "\0")
{
//sending disconnected event
evDisconnected();
}

if (content.IndexOf("\0") > -1)
{
//we have all the data

msg = msg.Substring(0, msg.IndexOf("\0"));
//received message, sending event
evMessageRead(msg);

msg = string.Empty;
}

}
else
{
//sending disconnected event
evDisconnected();
}
}
catch
{
//sending disconnected event
evDisconnected();
}
}
}

msg is a private string member i use for collecting the part of the message. I use a buffer size of 1024.
I'm looking forward reading your answers! Thank you for any help!