Damn Daniel Damn Daniel - 3 months ago 14
C# Question

Server/Client Socket Connection issue

--------------Removed----------------

Answer

Replace

while (text != "")
{
    // .....................
    if (current.Connected)
        current.BeginReceive(buffer, 0, 100, SocketFlags.None, ReceiveCallback, current);
}
if (current.Connected)
    current.BeginReceive(buffer, 0, 100, SocketFlags.None, ReceiveCallback, current);

with

if (text != "")
{
    // .....................
    // Notice there's no BeginReceive() here!
}
if (current.Connected)
    current.BeginReceive(buffer, 0, 100, SocketFlags.None, ReceiveCallback, current);

The function passed as callback paramater to BeginReceive() normally should not call BeginReceive() in a loop "until session is over". Instead it should call the BeginReceive() only once to pend another read operation if further data is expected (i.e. "session in not over").