2013-02-25 29 views
0

我有2個類(ClientServer)用於在我的應用程序中實現簡單通信。我的代碼如下所示:調試套接字通信程序

服務器:

public class Server { 
    public static void main(String[] ar) { 
     int port = 1025; // just a random port. make sure you enter something between 1025 and 65535. 
     try { 
      ServerSocket ss = new ServerSocket(port); // create a server socket and bind it to the above port number. 
      System.out.println("Waiting for a client..."); 
      Socket socket = ss.accept(); 
      InputStream sin = socket.getInputStream(); 
      OutputStream sout = socket.getOutputStream(); 
      DataInputStream in = new DataInputStream(sin); 
      DataOutputStream out = new DataOutputStream(sout); 
      BufferedReader keyboard = new BufferedReader(new InputStreamReader(
        System.in)); 
      System.out.println("enter meter id "); 
      String line = null; 

      while (true) { 
       line = in.readUTF(); // wait for the client to send a line of text. 
       System.out.println("client send me this id number " + line); 
       line = keyboard.readLine(); 
       out.writeUTF(line); 
       out.flush(); 
       //line = in.readUTF(); 
       System.out.println("Waiting for the next line..."); 
       System.out.println(); 
      } 
     } catch (Exception x) { 
      x.printStackTrace(); 
     } 
    } 
} 

客戶:

public class Client { 
    public static void main(String[] ar) { 
     int serverPort = 1025; 
     String address = "localhost"; 
     try { 
      InetAddress ipAddress = InetAddress.getByName(address); // create an object that represents the above IP address. 
      System.out.println(" IP address " + address + " and port " 
        + serverPort); 
      Socket socket = new Socket(ipAddress, serverPort); // create a socket with the server's IP address and server's port. 
      InputStream sin = socket.getInputStream(); 
      OutputStream sout = socket.getOutputStream(); 
      DataInputStream in = new DataInputStream(sin); 
      DataOutputStream out = new DataOutputStream(sout); 
      // Create a stream to read from the keyboard. 
      BufferedReader keyboard = new BufferedReader(new InputStreamReader(
        System.in)); 
      String line = null; 
      System.out.println("ClientConnected."); 
      System.out.println("enter meter id"); 

      while (true) { 
       line = keyboard.readLine(); // wait for the user to type in something and press enter. 
       System.out.println("Sending this number to the server..."); 
       out.writeUTF(line); // send the above line to the server. 
       out.flush(); // flush the stream to ensure that the data reaches the other end. 
       line = in.readUTF(); // wait for the server to send a line of text. 
       System.out 
         .println("The server was very polite. It sent me this : " 
           + line); 
       System.out.println(); 
      } 
     } 
     catch (Exception x) { 
      x.printStackTrace(); 
     } 
    } 
} 

我的問題是,雖然測試程序我得到的之間的通信客戶端和服務器,但在調試時,在上有一個斷點10行Server.java,它沒有去預定的目的地。該預定目的地是line = in.readUTF();Client.java。任何人都可以幫我解決這個問題嗎?

+0

首先,當使用localhost進行客戶端/服務器測試時,應該使用IP地址127.0.0.x,因爲「localhost」會在各種情況下引發奇怪的問題。其次,你能告訴我們你*得到了什麼嗎? – L0j1k 2013-02-25 12:06:59

+1

@ L0j1k:你會舉一些本地主機「怪異問題」的例子嗎?使用'localhost'是一個完全可以接受的事情,除非你有意在你的機器網絡配置上做了一些糟糕的事情,否則不會給127.0.0.1產生不同的結果。 – 2013-02-25 12:09:53

+0

有目的?不需要。您不需要有目的地「爲網絡配置做一些糟糕的事情」,因爲某些源代碼無法解析「本地主機」。考慮不執行任何主機名解析的源代碼。然後怎樣呢? – L0j1k 2013-02-25 12:16:28

回答

0

這是很好的做法,打開OutputStream小號之前InputStream S,你的插座,在此question說。

這個question也闡明瞭這一點。

0

我在這裏懷疑您的客戶端和服務器是在兩個不同的JVM進程中運行,並且java調試器無法同時調試兩個JVM。