2013-02-25 28 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。