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

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

4

2 回答 2

0

如this question所述,在您的套接字上打开OutputStreams之前的s是一种很好的做法。InputStream

这个问题也说明了这一点。

于 2013-02-25T12:30:24.547 回答
0

我在这里怀疑的是您的客户端和服务器在两个不同的 JVM 进程中运行,并且 java 调试器无法同时调试两个 JVM。

于 2016-09-19T12:31:52.507 回答