0

アプリケーションで単純な通信を実装するために使用される2 つのクラス (Clientと) があります。Server私のコードを以下に示します。

サーバ:

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

この質問で述べたように、ソケットでsのOutputStreamsを開くことをお勧めします。InputStream

この質問はまたそれを明らかにします。

于 2013-02-25T12:30:24.547 に答える
0

ここで私が疑うのは、クライアントとサーバーが 2 つの異なる JVM プロセスで実行されており、Java デバッガーが 2 つの JVM を同時にデバッグできないことです。

于 2016-09-19T12:31:52.507 に答える