客户端刷新连接上的C#套接字ErrorCode 10054

时间:2015-05-09 21:07:58

标签: c# .net sockets asynchronous

我一直在尝试使用TCP为我正在构建的应用程序编写通信层。我控制服务器端和客户端操作。

我遇到的问题是当我使用' localhost'作为我的知识产权,我没有问题。然而,当我使用互联网连接时,一切都神奇地工作......第一次。当客户端尝试与服务器建立另一个套接字连接时,在ReadCallback期间,我在服务器端获得SocketException ErrorCode 10054,特别是当我执行socket.EndReceive操作时。

我已经看过我的套接字没有得到正确处理的可能性 - 他们肯定应该这样,因为我看不到客户端或服务器端应用程序的路径,这些路径不会导致关闭。< / p>

我的代码非常类似于MSDN中的代码,可以找到here (server)here (client)

以下是我的客户代码的相关部分:

public class ClientStateObject
{
    // Client socket.
    public Socket workSocket = null;
    // Receive buffer.
    public byte[] buffer = new byte[Constants.ClientBuffer];
    // Received data string.
    public StringBuilder sb = new StringBuilder();

    public List<byte[]> Data = new List<byte[]>();
}

public class AsynchronousClient
{

    // ManualResetEvent instances signal completion.
    private static ManualResetEvent connectDone =
        new ManualResetEvent(false);
    private static ManualResetEvent sendDone =
        new ManualResetEvent(false);
    private static ManualResetEvent receiveDone =
        new ManualResetEvent(false);

    // The response from the remote device.
    private static byte[] response = null;

    public static object StartClient(object sendObj)
    {
        // Connect to a remote device.
        try
        {
            // Establish the remote endpoint for the socket.
            // The name of the 
            // remote device is ...
            IPHostEntry ipHostInfo = Dns.Resolve(Constants.Hostname); // localhost
            IPAddress ipAddress = ipHostInfo.AddressList[0];
            IPEndPoint remoteEP = new IPEndPoint(ipAddress, Constants.Port);

            // Create a TCP/IP socket.
            Socket client = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);



            LingerOption lo = new LingerOption(false, 0);
            client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lo);



            // Connect to the remote endpoint.
            client.BeginConnect(remoteEP,
                new AsyncCallback(ConnectCallback), client);
            connectDone.WaitOne();

            // Send test data to the remote device.
            object[] s;
            s = new object[2] { sendObj, "<EOF>" };
            Send(client, s);
            sendDone.WaitOne();

            // Receive the response from the remote device.
            Receive(client);
            receiveDone.WaitOne();

            // Write the response to the console.
            Console.WriteLine("Response received : {0}", response);
            //Console.ReadKey();


            // Release the socket.
            client.Shutdown(SocketShutdown.Both);

            client.Close();


            //object res = Deserialize(Deserialize(response) as byte[]);
            object res = Deserialize(Deserialize(response) as byte[]);
            return res;

        }
        catch (Exception e)
        {
            return e;
        }
    }

    private static void ConnectCallback(IAsyncResult ar)
    {
        try
        {
            // Retrieve the socket from the state object.
            Socket client = (Socket)ar.AsyncState;

            // Complete the connection.
            client.EndConnect(ar);

            Console.WriteLine("Socket connected to {0}",
                client.RemoteEndPoint.ToString());

            // Signal that the connection has been made.
            connectDone.Set();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

    private static void Receive(Socket client)
    {
        try
        {
            // Create the state object.
            ClientStateObject state = new ClientStateObject();
            state.workSocket = client;

            // Begin receiving the data from the remote device.
            client.BeginReceive(state.buffer, 0, Constants.ClientBuffer, 0,
                new AsyncCallback(ReceiveCallback), state);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

    private static void ReceiveCallback(IAsyncResult ar)
    {
        try
        {
            // Retrieve the state object and the client socket 
            // from the asynchronous state object.
            ClientStateObject state = (ClientStateObject)ar.AsyncState;
            Socket client = state.workSocket;

            // Read data from the remote device.
            int bytesRead = client.EndReceive(ar);

            if (bytesRead > 0)
            {
                // There might be more data, so store the data received so far.
                state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
                state.Data.Add(state.buffer); // Concat byte[] to received packet

                // Get the rest of the data.
                client.BeginReceive(state.buffer, 0, Constants.ClientBuffer, 0,
                    new AsyncCallback(ReceiveCallback), state);
            }
            else
            {
                // All the data has arrived; put it in response.
                if (state.sb.Length > 1)
                {
                    response = ConcatByteListToArray(state.Data);
                }
                // Signal that all bytes have been received.
                receiveDone.Set();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

    private static byte[] ConcatByteListToArray(List<byte[]> b)
    {
        List<byte> bList = new List<byte>();
        foreach (byte[] bA in b)
        {
            foreach (byte by in bA)
            {
                bList.Add(by);
            }
        }

        return bList.ToArray();
    }

    private static void Send(Socket client, object data)
    {
        // Convert the string data to byte data using ASCII encoding.

        byte[] byteData = Serialize(data);

        // Begin sending the data to the remote device.
        client.BeginSend(byteData, 0, byteData.Length, 0,
            new AsyncCallback(SendCallback), client);
    }

    private static void SendCallback(IAsyncResult ar)
    {
        try
        {
            // Retrieve the socket from the state object.
            Socket client = (Socket)ar.AsyncState;

            // Complete sending the data to the remote device.
            int bytesSent = client.EndSend(ar);
            Console.WriteLine("Sent {0} bytes to server.", bytesSent);

            // Signal that all bytes have been sent.
            sendDone.Set();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

    /*
     * Function: Serialize(object obj)
     * Requires: Arbitrary object.
     * Returns: MyMessage object, which constants a byte[] (Data) representing an object in TCP-friendly form.
     * Description: Private function used to turn an object into a serialized byte[].
     */
    private static byte[] Serialize(object obj)
    {
        using (var memoryStream = new MemoryStream())
        {
            (new BinaryFormatter()).Serialize(memoryStream, obj);
            return memoryStream.ToArray();
        }
    }

    /*
     * Function: Deserialize(MyMessage message)
     * Requires: MyMessage object with Data attribute.
     * Returns: object which was originally serialized.
     * Description: Private function used to reverse the process of "Serialize".
     */
    private static object Deserialize(byte[] message)
    {
        using (var memoryStream = new MemoryStream(message))
        {
            return (new BinaryFormatter()).Deserialize(memoryStream);
        }
    }

以下是服务器代码的相关部分:

public class ServerStateObject
{
    // Client  socket.
    public Socket workSocket = null;
    // Receive buffer.
    public byte[] buffer = new byte[Constants.ServerBuffer];
    // Received data string.
    public StringBuilder sb = new StringBuilder();

    public List<byte[]> Data = new List<byte[]>();
}

public class AsynchronousSocketListener
{
    public static bool GetDataSuccess;
    // Thread signal.
    public static ManualResetEvent allDone = new ManualResetEvent(false);

    public AsynchronousSocketListener()
    {

    }

    public static void StartListening()
    {
        // Data buffer for incoming data.
        byte[] bytes = new Byte[Constants.ServerBuffer];

        // Establish the local endpoint for the socket.
        // The DNS name of the computer
        // running the listener is "host.contoso.com".

        IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, Constants.Port);

        // Create a TCP/IP socket.
        Socket listener = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream, ProtocolType.Tcp);

        LingerOption lo = new LingerOption(false, 0);
        listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lo);



        // Bind the socket to the local endpoint and listen for incoming connections.
        try
        {
            listener.Bind(localEndPoint);
            listener.Listen(10);

            while (true)
            {
                // Set the event to nonsignaled state.
                allDone.Reset();

                // Start an asynchronous socket to listen for connections.
                Console.WriteLine("Waiting for a connection...");
                listener.BeginAccept(
                    new AsyncCallback(AcceptCallback),
                    listener);

                // Wait until a connection is made before continuing.
                allDone.WaitOne();

                //Console.WriteLine("Done waiting for client.");
            }

        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }

        Console.WriteLine("\nPress ENTER to continue...");
        Console.Read();

    }

    public static void AcceptCallback(IAsyncResult ar)
    {
        // Signal the main thread to continue.
        allDone.Set();

        // Get the socket that handles the client request.
        Socket listener = (Socket)ar.AsyncState;
        Socket handler = listener.EndAccept(ar);

        // Create the state object.
        ServerStateObject state = new ServerStateObject();
        state.workSocket = handler;
        handler.BeginReceive(state.buffer, 0, Constants.ServerBuffer, 0,
            new AsyncCallback(ReadCallback), state);
    }

    public static void ReadCallback(IAsyncResult ar)
    {
        String content = String.Empty;

        // Retrieve the state object and the handler socket
        // from the asynchronous state object.
        ServerStateObject state = (ServerStateObject)ar.AsyncState;
        Socket handler = state.workSocket;
        Console.WriteLine("Read {0} bytes from {1} : {2}.",
                    content.Length, (handler.LocalEndPoint as IPEndPoint).Address, (handler.LocalEndPoint as IPEndPoint).Port);
        int bytesRead = 0;
        try
        {
            bytesRead = handler.EndReceive(ar);
        }
        catch (SocketException se)
        {
            Console.WriteLine("Socket Exception in Callback Read: {0}", se.ErrorCode); // I always get an error here on refresh
        }

        // Read data from the client socket. 
        if (bytesRead > 0)
        {
            // There  might be more data, so store the data received so far.
            state.sb.Append(Encoding.ASCII.GetString(
                state.buffer, 0, bytesRead)); // Check if there is an EOF in the byte[]
            state.Data.Add(state.buffer); // Concat byte[] to received packet


            // Check for end-of-file tag. If it is not there, read 
            // more data.
            content = state.sb.ToString();
            if (content.IndexOf("<EOF>") > -1)
            {
                byte[] rArray = ConcatByteListToArray(state.Data);
                byte[] sendArray = Serialize(HandleRead(rArray));
                // All the data has been read from the 
                // client. Display it on the console.
                Console.WriteLine("Read {0} bytes from {1} : {2}.",
                    content.Length, (handler.LocalEndPoint as IPEndPoint).Address, (handler.LocalEndPoint as IPEndPoint).Port);

                // Echo the data back to the client.
                Send(handler, sendArray);
            }
            else
            {
                // Not all data received. Get more.
                handler.BeginReceive(state.buffer, 0, Constants.ServerBuffer, 0,
                new AsyncCallback(ReadCallback), state);
            }


        }
    }

    private static void Send(Socket handler, object data)
    {
        // Convert the string data to byte data using ASCII encoding.
        byte[] byteData = Serialize(data);

        // Begin sending the data to the remote device.
        handler.BeginSend(byteData, 0, byteData.Length, 0,
            new AsyncCallback(SendCallback), handler);
    }

    private static void SendCallback(IAsyncResult ar)
    {
        try
        {
            // Retrieve the socket from the state object.
            Socket handler = (Socket)ar.AsyncState;

            // Complete sending the data to the remote device.
            int bytesSent = handler.EndSend(ar);
            Console.WriteLine("Sent {0} bytes to client.", bytesSent);

            handler.Shutdown(SocketShutdown.Both);
            handler.Close();

            Console.WriteLine("Connection to client broken.");

        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

我认为问题可能与Windows处理套接字的方式有关,也许还没有那么快。遗憾的是,即使我在第一次和第二次连接之间等待几分钟,代码也无法进行刷新。

1 个答案:

答案 0 :(得分:2)

好的,经过我的代码搜索和阅读错误消息后,我发现客户端在进入“发送”之前抛出了错误代码10057。此错误意味着套接字未打开 - 即使套接字应该等待发送直到套接字打开。

因此,问题是事件在程序的每次运行开始时都没有重置。这在localhost上不是问题,因为异步套接字能够在到达需要打开的send函数中的某个点之前打开。但是,当终点不是本地时,它花了太长时间,因此错误。

我只是在“StartClient”的开头添加了三行代码:

            connectDone.Reset();
            sendDone.Reset();
            receiveDone.Reset();

这解决了整个问题。

所以...总是记得设置你的初始变量条件,以免你看起来像个傻瓜!