C# - 通过套接字

时间:2016-12-01 10:55:07

标签: c# sockets server client

我和我的朋友正在两台不同的计算机上制作基本的客户端/服务器应用程序。我们要做的是:

1.我给他发了一个字符串(作为服务器)

2他送回我另一个字符串,

  1. 我编辑并发回。
  2. 但每次我们到达第3部分他都不会回复我的软件只是停止工作并通过调试我最终会

    "{"A request to send or receive data was disallowed because the socket had already been shut down in that direction with a previous shutdown call"} 
    

    我很确定故障是在我的最后,这是我正在使用的功能:

    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();
            sendDone.Set();
    
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }
    
    private static void Send(Socket handler, String data)
    {
        // Convert the string data to byte data using ASCII encoding.
        byte[] byteData = Encoding.ASCII.GetBytes(data);
    
        // Begin sending the data to the remote device.
        handler.BeginSend(byteData, 0, byteData.Length, 0,
            new AsyncCallback(SendCallback), handler);
    }
    
    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)
        {
            Console.WriteLine(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 = s.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.
                s.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)
        {
            Console.WriteLine(e.ToString());
        }
    }
    

    我通过

    与客户联系
      IPAddress ipAd = IPAddress.Parse("my_actual_ip_adress");
            // use local m/c IP address, and 
            // use the same in the client
    
            /* Initializes the Listener */
            TcpListener myList = new TcpListener(ipAd, 8001);
    
            /* Start Listeneting at the specified port */
            myList.Start();
    

    我意识到这可能听起来像是一个愚蠢的问题,但我们很难找到实际的解决方案,任何帮助都会让我非常感激

1 个答案:

答案 0 :(得分:0)

我设法解决了!原来我们需要摆脱handler.Shutdown(SocketShutdown.Both),然后改变客户端接收数据的方式,因为他正在等待套接字关闭。
- 一些