0

以下のプログラムの流れを知る必要があります。

クライアントクラス:

1. Socket sock = new Socket("localhost", 1101);
2. PrintWriter write = new PrintWriter(sock.getOutputStream());
3. write.println("Hello");
4. write.close();

サーバークラス:

1. ServerSocket sersock = new ServerSocket(1101);
2. sock = sersock.accept();
3. InputStreamReader read = new InputStreamReader(sock.getInputStream());
4. BufferedReader buf = new BufferedReader(read);
5. System.out.println(buf.readLine());
6. buf.close();

サーバー クラスを実行してからクライアント クラスを実行すると、プログラムの流れはどのように機能しますか?

4

1 に答える 1

2

It flows exactly how it reads.

Server binds the socket to port 1101, listens for connections

1. ServerSocket sersock = new ServerSocket(1101);
2. sock = sersock.accept();

Client connects to server and sends "hello" and closes connection.

1. Socket sock = new Socket("localhost", 1101);
2. PrintWriter write = new PrintWriter(sock.getOutputStream());
3. write.println("Hello");
4. write.close();

Server reads and prints line from input stream after a connection has been made then closes the reader.

3. InputStreamReader read = new InputStreamReader(sock.getInputStream());
4. BufferedReader buf = new BufferedReader(read);
5. System.out.println(buf.readLine());
6. buf.close();
于 2013-10-01T04:53:38.307 に答える