I've finally finished my TCP server and it's all working fine on my personal machine (which I used to write and test the program) which is Windows 7.

However, when transferring the program to my VPS (which is Windows XP) the program gives the following exception when attempting to receive data for about 49/50 attempts to connect by the client (randomly works every now and again):

Exception thrown on 9/22/2010 4:04:36 PM: System.Net.Sockets.SocketException (0x80004005): The I/O operation has been aborted because of either a thread exit or an application request
   at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
   at AnonMTS.Server.ClientConnection.Receive(IAsyncResult result)

This simply happens when EndReceive is called (BeginReceieve and such are correctly called before and it all works on my first machine), and I just simply don't understand why.

Both machines have the .NET framework 4.0 and the .NET MySQL connector which are as far as I know the only files needed for the program to run...

Dani AI

Generated

As discovered, the exception coming from EndReceive (error code 995) means the asynchronous read was aborted — most commonly because the receive completed after you tried to finish it, or because the socket was closed while the operation was pending. was right to point out checking SocketException.ErrorCode as the first diagnostic step; that confirms whether the error is an aborted I/O versus a different socket error.

A robust pattern is to call EndReceive only from the async callback (or after a confirmed wait on the IAsyncResult). This avoids racing EndReceive against completion. Example callback pattern:

private void ReceiveCallback(IAsyncResult ar)
{
    var state = (State)ar.AsyncState;
    var socket = state.WorkSocket;

    int bytesRead;
    try
    {
        bytesRead = socket.EndReceive(ar);
    }
    catch (SocketException ex)
    {
        // log ex.ErrorCode and handle clean-up
        return;
    }

    if (bytesRead == 0)
    {
        // remote closed connection
        socket.Close();
        return;
    }

    // process bytes in state.Buffer, then continue receiving
    socket.BeginReceive(state.Buffer, 0, state.Buffer.Length, SocketFlags.None,
                        ReceiveCallback, state);
}

Practical tips and cautions: always call EndReceive exactly once for each BeginReceive; do not call it after disposing/closing the socket; if you use ar.AsyncWaitHandle.WaitOne() to block, give a reasonable timeout and call ar.AsyncWaitHandle.Close() (or Dispose) afterward to avoid a handle leak; avoid blocking ThreadPool threads — prefer the callback or Task-based APIs (e.g., NetworkStream.ReadAsync / Socket.ReceiveAsync) for cleaner flow. Log ex.ErrorCode and treat bytesRead == 0 as a graceful remote close. These checks eliminate the race that commonly shows up on one machine but not another.

Recommended Answers

All 2 Replies

From MSDN documentation on the Socket class:

If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.

=_=...

Stupid question really, sorry.

The error code was 995 which I fixed by adding a WaitOne() call to the result gotten from the BeginReceive function =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.