客户端/服务器套接字数据丢失

本文关键字:数据 套接字 服务器 客户端 | 更新日期: 2023-09-27 18:21:25

我试图用MSDN中的示例编写自己的应用程序(https://msdn.microsoft.com/en-us/library/6y0e13d3%28v=vs.110%29.aspx)

任务是向服务器发送一个对象,并获得它收到的反馈。我有一个表格,我点击按钮将信息发送到服务器。我的问题是,如果我启动服务器和客户端,我会点击按钮-我发送一个对象并从服务器获得反馈,如果我再次点击按钮,有时它会起作用,有时我会得到:

Sent 595 bytes to server.
Response received : Mustermann
Exception thrown: 'System.ObjectDisposedException' in System.dll
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.Sockets.Socket'.
   at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult, SocketError& errorCode)
   at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
   at Zakinator.AsynchronousClient.ReceiveCallback(IAsyncResult ar) in C:'Data'1_Progs'Me'Zakinator'Zakinator'Client.cs:line 144

在带有Buttonclick的Form上,我只调用Client StartClient(Communicator Communicator,WriteLog WriteLog)中的函数

我的客户代码:

namespace Zakinator
{
    public delegate void WriteLog(string msg);
    // State object for receiving data from remote device.
    public class StateObject
    {
        // Client socket.
        public Socket workSocket = null;
        // Size of receive buffer.
        public const int BufferSize = 256;
        // Receive buffer.
        public byte[] buffer = new byte[BufferSize];
        // Received data string.
        public StringBuilder sb = new StringBuilder();
    }
    public class AsynchronousClient
    {
        private static WriteLog wl;
        // The port number for the remote device.
        private const int port = 11000;
        // 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 String response = String.Empty;
        public static void StartClient(Communicator communicator,WriteLog writeLog)
        {
            wl = writeLog;
            // Connect to a remote device.
            try
            {
                // Establish the remote endpoint for the socket.
                // The name of the 
                // remote device is "host.contoso.com".
                IPHostEntry ipHostInfo = Dns.Resolve("localhost");
                IPAddress ipAddress = ipHostInfo.AddressList[0];
                IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
                // Create a TCP/IP socket.
                Socket client = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp);
                // Connect to the remote endpoint.
                client.BeginConnect(remoteEP,
                    new AsyncCallback(ConnectCallback), client);
                connectDone.WaitOne();
                // Send test data to the remote device.
                Send(client, communicator);
                //sendDone.WaitOne();
                // Receive the response from the remote device.
                Receive(client);
                receiveDone.WaitOne();
                // Write the response to the console.
                Debug.Print(String.Format("Response received : {0}", response));
                // Release the socket.
                client.Shutdown(SocketShutdown.Both);
                client.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
        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);
                Debug.Print(String.Format("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.
                StateObject state = new StateObject();
                state.workSocket = client;
                // Begin receiving the data from the remote device.
                client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                    new AsyncCallback(ReceiveCallback), state);
            }
            catch (Exception e)
            {
                Debug.Print(e.ToString());
            }
        }
        private static void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                // Retrieve the state object and the client socket 
                // from the asynchronous state object.
                StateObject state = (StateObject)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));
                    // Get the rest of the data.
                    client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,new AsyncCallback(ReceiveCallback), state);
                }
                else
                {
                    // All the data has arrived; put it in response.
                    if (state.sb.Length > 1)
                    {
                        response = state.sb.ToString();
                    }
                    // Signal that all bytes have been received.
                    receiveDone.Set();
                }
            }
            catch (Exception e)
            {
                Debug.Print(e.ToString());
            }
        }
        private static void Send(Socket client, byte[] byteData)
        {
            // Begin sending the data to the remote device.
            client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), client);
        }
        private static void Send(Socket client, Communicator data)
        {
            byte[] byteData;
            MemoryStream ms = new MemoryStream();
            BinaryFormatter bf1 = new BinaryFormatter();
            bf1.Serialize(ms, data);
            byteData = ms.ToArray();
            Send(client, BitConverter.GetBytes(byteData.Length));
            sendDone.WaitOne();
            Send(client, byteData);
            sendDone.WaitOne();
        }

        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);
                Debug.Print(String.Format("Sent {0} bytes to server.", bytesSent));
                // Signal that all bytes have been sent.
                sendDone.Set();
            }
            catch (Exception e)
            {
                Debug.Print(e.ToString());
            }
        }
    }
}

我的服务器代码:

namespace ServerZakinator
{

    // State object for reading client data asynchronously
    public class StateObject
    {
        // Client  socket.
        public Socket workSocket = null;
        // Size of receive buffer.
        public const int BufferSize = 1024;
        // Receive buffer.
        public byte[] buffer = new byte[BufferSize];
        // Received data string.
        public StringBuilder sb = new StringBuilder();
        // incoming image size
        public int ObjectSize;
    }
    public class AsynchronousSocketListener
    {
        // Thread signal.
        public static ManualResetEvent allDone = new ManualResetEvent(false);
        private static System.Timers.Timer SentCommunicatorToInfo = new System.Timers.Timer();
        private static byte[] _objectBuff;
        private static int _totBytesRead = 0;
        public static int Port=11000;
        public static string Host = "127.0.0.1";
        private static List<Communicator> CommunicatorsList = new List<Communicator>();
        public delegate void DataReceived(Communicator communicator);
        public static event DataReceived NewData;
        protected static void SendToWinwow(Communicator communicator)
        {
            CommunicatorsList.Add(communicator);
        }
        public AsynchronousSocketListener()
        {
        }
        public static void StartListening()
        {
            //SetUp Timer
            SentCommunicatorToInfo.Interval = 1000;
            SentCommunicatorToInfo.Enabled = true;
            SentCommunicatorToInfo.Elapsed += new ElapsedEventHandler(OnTimedEvent);

            // Data buffer for incoming data.
            byte[] bytes = new Byte[1024];
            // Establish the local endpoint for the socket.
            // The DNS name of the computer
            // running the listener is "host.contoso.com".
            IPHostEntry ipHostInfo = Dns.GetHostEntry(Host);
            IPAddress ipAddress = ipHostInfo.AddressList[0];
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse(Host), Port);
            // Create a TCP/IP socket.
            Socket listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
            // Bind the socket to the local endpoint and listen for incoming connections.
            try
            {
                listener.Bind(localEndPoint);
                listener.Listen(100);
                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();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            Console.WriteLine("'nPress ENTER to continue...");
            Console.Read();
        }
        private static void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            if (NewData != null)
            {
                var item = CommunicatorsList.FirstOrDefault();
                if (item == null)
                    return;
                NewData(item);
                CommunicatorsList.Remove(item);
            }
        }

        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.
            StateObject state = new StateObject();
            state.workSocket = handler;
            handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,new AsyncCallback(ReadHeaderCallback), state);
        }
        public static void ReadHeaderCallback(IAsyncResult ar)
        {
            // Retrieve the state object and the handler socket
            // from the asynchronous state object.
            StateObject state = (StateObject)ar.AsyncState;
            Socket handler = state.workSocket;
            // Read data from the client socket. 
            int bytesRead = handler.EndReceive(ar);
            //we need to add error handler here...but later
            state.ObjectSize = BitConverter.ToInt32(state.buffer, 0);
            _objectBuff = new byte[state.ObjectSize];
            _totBytesRead = 0;
            handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 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.
            StateObject state = (StateObject)ar.AsyncState;
            Socket handler = state.workSocket;
            // Read data from the client socket. 
            int bytesRead = handler.EndReceive(ar);

            if (bytesRead > 0)
            {
                // There  might be more data, so store the data received so far.
                Buffer.BlockCopy(state.buffer, 0, _objectBuff, _totBytesRead, bytesRead);
                _totBytesRead += bytesRead;
                if (_totBytesRead < state.ObjectSize)
                {
                    // Not all data received. Get more.
                    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                    new AsyncCallback(ReadCallback), state);
                }
                else
                {
                    MemoryStream ms = new MemoryStream(_objectBuff);
                    BinaryFormatter bf1 = new BinaryFormatter();
                    ms.Position = 0;
                    object rawObj = bf1.Deserialize(ms);
                    Communicator msgObj = (Communicator)rawObj;
                    SendToWinwow(msgObj);
                    Send(handler, msgObj);
                }
            }
        }
        private static void Send(Socket handler, Communicator communicator)
        {
            // Convert the string data to byte data using ASCII encoding.
            byte[] byteData = Encoding.ASCII.GetBytes(communicator.Patient.Name);
            // 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);
                Debug.Print("Sent {0} bytes to client.", bytesSent);
                handler.Shutdown(SocketShutdown.Both);
                handler.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
}

客户端/服务器套接字数据丢失

Receive(client);

立即返回。当调用ReceiveCallback时,您的客户端已经在StartClient方法中关闭。