2016-12-01 169 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關閉。
    - 部分