Java Socket官方文档学习(一)

Java Socket官方文档学习(一)

今天尝试用Java重写字节青训营的GO项目SOCK5代理 学习Java Socket包,部分内容取自Orcale官方文档由我翻译。

What Is a Socket? // 什么是Socket

A socket is one end-point of a two-way communication link between two programs running on the network.
Socket classes are used to represent the connection between a client program and a server program.
The java.net package provides two classes–Socket and ServerSocket
–that implement the client side of the connection and the server side of the connection, respectively.

一个Socket是两个运行在网络上的程序通讯链接的一个端点。Socket类被用于连接一个客户端程序和一个服务端程序。
Java.net 这个包提供了两个类即Socket和ServerSocket。这两个类分别实现了客户端和服务端的链接。

Reading from and Writing to a Socket // 这个页面是一个关于Socket的例子 源码参见那个Echo Client的链接。文章中的代码都节选自那里。

String hostName = args[0];
int portNumber = Integer.parseInt(args[1]);

try (
    Socket echoSocket = new Socket(hostName, portNumber);        // 1st statement
    PrintWriter out =                                            // 2nd statement
        new PrintWriter(echoSocket.getOutputStream(), true);
    BufferedReader in =                                          // 3rd statement 
        new BufferedReader(
            new InputStreamReader(echoSocket.getInputStream()));
    BufferedReader stdIn =                                       // 4th statement 
        new BufferedReader(
            new InputStreamReader(System.in))
)

文档说这个例子实现了一个EchoClient //这是EchoClient代码的官方链接https://docs.oracle.com/javase/tutorial/networking/sockets/examples/EchoClient.java

//这是Echo Protocal https://www.rfc-editor.org/rfc/rfc862

//所谓Echo Client翻译过来就是回声客户端,在链接建立以后它会发回所有它收到的信息,直到用户关闭链接。

The EchoClient example creates a socket, thereby getting a connection to the echo server. It reads input from the user on the standard input stream, and then forwards that text to the echo server by writing the text to the socket. The server echoes the input back through the socket to the client. The client program reads and displays the data passed back to it from the server.

这个回声客户端的例子创造了一个Socket,它建立了一个到echo server的链接。它通过标准输入流读取用户的输入,然后通过写入这些text到socket来发送这些text到回声服务器。随后回声服务器会把这些输入通过socket返回到客户端。客户端程序读取、显示这些从服务端被发来的数据。

看案例代码 Socket类创建了一个Socket实例,参数是传入的hostName和portNumber

BufferReader是Java io包中的类,下一篇笔记写。 PrintWriter不知掉是做什么,下一篇跟BufferReader一起写。

看文档似乎是PrintWriter和BufferReader通过Socket类中的getInputStream()方法获得输出流和输入流的内容。

The second statement in the try-with resources statement gets the socket’s output stream and opens a PrintWriter on it named out. Similarly, the third statement gets the socket’s input stream and opens a BufferedReader on it named in. The example uses readers and writers so that it can write Unicode characters over the socket. If you are not yet familiar with the Java platform’s I/O classes, you may wish to read Basic I/O.

文档中说第二个声明中获得了Socket的输出流然后在其上打开了一个PrintWriter。相似的,第三个声明获得了Socket的输入流然后在其上打开了一个BufferedReader。这个例子使用了reader和writer使得它可以在socket中写入unicode字符。

The next interesting part of the program is the while loop. The loop reads a line at a time from the standard input stream with the BufferedReader object stdIn, which is created in the fourth statement in the try-with resources statement. The loop then immediately sends the line to the server by writing it to the PrintWriter connected to the socket:

String userInput;
while ((userInput = stdIn.readLine()) != null) {
    out.println(userInput);
    System.out.println("echo: " + in.readLine());
}

文档说这个程序中另一个有趣的地方在于这个while循环,这个循环使用BufferedReader对象读取标准输入流的每一行。这个循环通过把这些读取的行写入链接到这个Socket的PrintWriter来把这些行送到服务器。