【问题标题】:Debugging a socket communication program调试套接字通信程序
【发布时间】:2013-02-10 13:43:07
【问题描述】:

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

我的问题是,在测试程序时,我确实获得了客户端和服务器之间的通信,但在调试时,在Server.javaout.flush 行上有一个断点,它没有到达预期的目的地。这个预期的目的地是Client.javaline = in.readUTF(); 行。谁能帮我解决这个问题?

【问题讨论】:

  • 首先,当使用 localhost 进行客户端/服务器测试时,您应该使用 IP 地址127.0.0.x,因为“localhost”会在各种情况下导致奇怪的问题。第二,你能告诉我们你得到了什么吗?
  • @L0j1k:你会举一些本地主机“奇怪问题”的例子吗?使用localhost 是完全可以接受的做法,并且不会产生与127.0.0.1 不同的结果,除非您故意对您的机器网络配置做了一些可怕的事情。
  • 故意的?不。您不需要故意对您的网络配置“做一些可怕的事情”,因为某些源代码无法解析“localhost”。考虑不进行任何主机名解析的源。然后呢?
  • 请为 SO 正确缩进您的代码。它看起来像狗的早餐。
  • 任何不知道如何解析“localhost”或将其解析为非环回地址或未启用环回网络接口的主机都严重错误配置。您可以将“localhost”的这种使用视为对网络设置以及其他内容的测试。 “现在做什么”应该是“修复网络配置”

标签: java network-programming


【解决方案1】:

最好在您的套接字上打开OutputStreams 之前 InputStreams,如question 中所述。

question 也说明了这一点。

【讨论】:

    【解决方案2】:

    我怀疑你的客户端和服务器在两个不同的 JVM 进程中运行,而 java 调试器不能同时调试两个 JVM。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-20
      • 2020-10-23
      相关资源
      最近更新 更多