2014-10-20 95 views
1

這是我的問題,所有從c#客戶端發送的消息都沒有被服務器接收,直到我關閉客戶端的套接字並最終服務器接收到一次所有數據。Socket問題c#客戶端到java服務器

C#客戶端

public static class AsynchronousClient 
{ 
    // The port number for the remote device. 
    private const int port = 8888; 

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

    public static Socket client; 
    // The response from the remote device. 
    private static String response = String.Empty; 

    public static void StartClient() 
    { 
     // 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.GetHostEntry("127.0.0.1"); 
      IPAddress ipAddress = ipHostInfo.AddressList[0]; 
      IPEndPoint remoteEP = new IPEndPoint(ipAddress, 8888); 

      // Create a TCP/IP 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, "test connection");  
      sentDown.WaitOne(); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.ToString()); 
     } 
    } 

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

    public static void Send(Socket client, 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. 
     client.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, 
      new AsyncCallback(SendCallback), null); 
    } 

    public static void SendCallback(IAsyncResult ar) 
    { 
     try 
     { 
      // Retrieve the socket from the state object. 

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

Java服務器端

public class Connection_to_port extends Thread { 

    final static int port = 8888; 
    private Socket socket; 
    String message = ""; 
    public static void main(String[] args) { 
    try { 
     ServerSocket socketServeur = new ServerSocket(port); 
     while (true) { 
     Socket socketClient = socketServeur.accept(); 
     Connection_to_port t = new Connection_to_port(socketClient); 
     t.start(); 
     System.out.println("Connected to client : " + socketClient.getInetAddress()); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 

    public Connection_to_port(Socket socket) { 
    this.socket = socket; 
    } 

    public void run() { 
    handleMessage(); 
    } 

    public void handleMessage() { 
    try { 
     BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
     message = in.readLine(); 
     System.out.println(message); 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

在我的客戶,我有一些數據發送到服務器這樣的

AsynchronousClient.Send(AsynchronousClient.client, "{myjsondata}");   

我的客戶僅僅是發送,不接收。

問題是,我的java服務器收不到任何東西!但客戶說它已發送,而我在Wireshark上看到它正在發送。

當我做
AsynchronousClient.client.Shutdown(SocketShutdown.Both);

最後,我同時看到服務器上的所有消息。就像我只發送一條消息一樣。

回答

2

Java端試圖讀取一行(您正在使用readLine)。

此方法不會返回,直到存在行尾字符或流關閉。

當您關閉客戶端時,實際上會關閉流。

您的測試消息不包含行尾字符,因此readLine返回的唯一方法是在流的末尾。

+0

謝謝你的工作,所以添加\ n是更好的方法,或者比readLine更好地處理來自客戶端的消息? – amdev 2014-10-20 14:52:57

+0

如果您的信息是純文本,則行尾是一個不錯的選擇。如果消息需要其他的東西,你有其他的選擇。對於N字節的固定長度記錄,從流中讀取N個字節。這一切都取決於你想發送什麼。 – RealSkeptic 2014-10-20 15:04:42