2016-12-01 118 views
1

我和我的朋友正在两台不同的计算机上制作一个基本的客户端/服务器应用程序。我们正在尝试做的:C# - 发送,reciving,然后再通过套接字发送

1.I送他一个字符串(服务器)

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

  • 我编辑它并将其发回。
  • 但每次我们到了第3部分,他不recive什么我的软件只是停止工作,并通过调试我结束了

    "{"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()); 
        } 
    } 
    

    我Connet的与客户端通过

    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(); 
    

    我意识到这可能听起来像一个愚蠢的问题,但我们有困难的时候提出实际的解决方案,任何帮助,我将不胜感激

    +0

    你为什么要调用'handler.Shutdown(SocketShutdown.Both);'?它做它所说的。 –

    +0

    客户端不接收第一个字符串如果我评论该行或尝试任何版本的SocketShutDown – some

    +1

    我设法修复它!原来我们需要摆脱handler.Shutdown(SocketShutdown.Both),然后改变客户端接收数据的方式,因为他在等待socket关闭。感谢让我沿着正确的道路@JeroenvanLangen – some

    回答

    0

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