Skip to content

Commit c74de6e

Browse files
author
likongpeng
committed
增加一些注释
1 parent 1170ff6 commit c74de6e

File tree

4 files changed

+165
-124
lines changed

4 files changed

+165
-124
lines changed

src/main/java/java/net/ServerSocket.java

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@
3232
import java.security.PrivilegedExceptionAction;
3333

3434
/**
35-
* This class implements server sockets. A server socket waits for
36-
* requests to come in over the network. It performs some operation
37-
* based on that request, and then possibly returns a result to the requester.
35+
*
36+
* 等待客户端的套接字发送信息过来,并在处理完成之后,把响应回传给客户端
37+
This class implements server sockets. A server socket waits for
38+
requests to come in over the network. It performs some operation
39+
based on that request, and then possibly returns a result to the requester.
3840
* <p>
3941
* The actual work of the server socket is performed by an instance
4042
* of the {@code SocketImpl} class. An application can
@@ -53,14 +55,12 @@ class ServerSocket implements java.io.Closeable {
5355
/**
5456
* Various states of this socket.
5557
*/
56-
private boolean created = false;
57-
private boolean bound = false;
58-
private boolean closed = false;
58+
private boolean created = false;// 已创建
59+
private boolean bound = false;// 绑定
60+
private boolean closed = false;// 已关闭
5961
private Object closeLock = new Object();
6062

61-
/**
62-
* The implementation of this Socket.
63-
*/
63+
// 底层的功能都依靠 SocketImpl 来实现
6464
private SocketImpl impl;
6565

6666
/**
@@ -131,15 +131,17 @@ public ServerSocket(int port) throws IOException {
131131
/**
132132
* Creates a server socket and binds it to the specified local port
133133
* number, with the specified backlog.
134+
* 如果 port 是 0 的话,表示 port 是自动分配
134135
* A port number of {@code 0} means that the port number is
135136
* automatically allocated, typically from an ephemeral port range.
136137
* This port number can then be retrieved by calling
137-
* {@link #getLocalPort getLocalPort}.
138+
{@link #getLocalPort getLocalPort}.
138139
* <p>
139-
* The maximum queue length for incoming connection indications (a
140-
* request to connect) is set to the {@code backlog} parameter. If
141-
* a connection indication arrives when the queue is full, the
142-
* connection is refused.
140+
* 最大的客户端连接数是由 backlog 来控制的,如果要作为构造器入参传入的话,应该是大于 0 的
141+
The maximum queue length for incoming connection indications (a
142+
request to connect) is set to the {@code backlog} parameter. If
143+
a connection indication arrives when the queue is full, the
144+
connection is refused.
143145
* <p>
144146
* If the application has specified a server socket factory, that
145147
* factory's {@code createSocketImpl} method is called to create
@@ -151,18 +153,18 @@ public ServerSocket(int port) throws IOException {
151153
* as its argument to ensure the operation is allowed.
152154
* This could result in a SecurityException.
153155
*
154-
* The {@code backlog} argument is the requested maximum number of
155-
* pending connections on the socket. Its exact semantics are implementation
156-
* specific. In particular, an implementation may impose a maximum length
157-
* or may choose to ignore the parameter altogther. The value provided
158-
* should be greater than {@code 0}. If it is less than or equal to
159-
* {@code 0}, then an implementation specific default will be used.
156+
The {@code backlog} argument is the requested maximum number of
157+
pending connections on the socket. Its exact semantics are implementation
158+
specific. In particular, an implementation may impose a maximum length
159+
or may choose to ignore the parameter altogther. The value provided
160+
should be greater than {@code 0}. If it is less than or equal to
161+
{@code 0}, then an implementation specific default will be used.
160162
* <P>
161163
*
162164
* @param port the port number, or {@code 0} to use a port
163165
* number that is automatically allocated.
164166
* @param backlog requested maximum length of the queue of incoming
165-
* connections.
167+
connections.
166168
*
167169
* @exception IOException if an I/O error occurs when opening the socket.
168170
* @exception SecurityException
@@ -227,13 +229,17 @@ public ServerSocket(int port, int backlog) throws IOException {
227229
* @since JDK1.1
228230
*/
229231
public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException {
232+
// 默认是 SocksSocketImpl 实现
230233
setImpl();
234+
// 端口必须大于 0,小于 65535
231235
if (port < 0 || port > 0xFFFF)
232236
throw new IllegalArgumentException(
233237
"Port value out of range: " + port);
238+
// 最大可连接数如果小于1,那么采取默认的 50
234239
if (backlog < 1)
235240
backlog = 50;
236241
try {
242+
// 底层 navtive 方法
237243
bind(new InetSocketAddress(bindAddr, port), backlog);
238244
} catch(SecurityException e) {
239245
close();
@@ -476,8 +482,9 @@ public SocketAddress getLocalSocketAddress() {
476482
}
477483

478484
/**
479-
* Listens for a connection to be made to this socket and accepts
480-
* it. The method blocks until a connection is made.
485+
*
486+
Listens for a connection to be made to this socket and accepts
487+
it. The method blocks until a connection is made.
481488
*
482489
* <p>A new Socket {@code s} is created and, if there
483490
* is a security manager,

src/main/java/java/net/Socket.java

Lines changed: 67 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,17 @@
5252
*/
5353
public
5454
class Socket implements java.io.Closeable {
55-
/**
56-
* Various states of this socket.
57-
*/
55+
56+
5857
// 套接字的状态
5958
private boolean created = false;// 已创建
6059
private boolean bound = false;// 已绑定
6160
private boolean connected = false;// 已连接
6261
private boolean closed = false;// 已关闭
63-
private Object closeLock = new Object();
64-
private boolean shutIn = false;
65-
private boolean shutOut = false;
62+
private Object closeLock = new Object();// 关闭锁
63+
private boolean shutIn = false;// 读是否关闭
64+
private boolean shutOut = false;// 写是否关闭
6665

67-
/**
68-
* The implementation of this Socket.
69-
*/
7066
// 套接字的实现
7167
SocketImpl impl;
7268

@@ -107,7 +103,7 @@ void setImpl() {
107103

108104
/**
109105
* Creates an unconnected socket, specifying the type of proxy, if any,
110-
* that should be used regardless of any other settings.
106+
that should be used regardless of any other settings.
111107
* <P>
112108
* If there is a security manager, its {@code checkConnect} method
113109
* is called with the proxy host address and port number
@@ -552,7 +548,7 @@ public void connect(SocketAddress endpoint) throws IOException {
552548
/**
553549
* Connects this socket to the server with a specified timeout value.
554550
* A timeout of zero is interpreted as an infinite timeout. The connection
555-
* will then block until established or an error occurs.
551+
will then block until established or an error occurs.
556552
*
557553
* @param endpoint the {@code SocketAddress}
558554
* @param timeout the timeout value to be used in milliseconds.
@@ -1026,11 +1022,17 @@ public boolean getTcpNoDelay() throws SocketException {
10261022
* @since JDK1.1
10271023
* @see #getSoLinger()
10281024
*/
1025+
// on 为 false,表示不启用延时关闭,true 的话表示启用延时关闭
1026+
// linger 为延时的时间,单位秒
10291027
public void setSoLinger(boolean on, int linger) throws SocketException {
1028+
// 检查是否已经关闭
10301029
if (isClosed())
10311030
throw new SocketException("Socket is closed");
1031+
// 不启用延时关闭
10321032
if (!on) {
10331033
getImpl().setOption(SocketOptions.SO_LINGER, new Boolean(on));
1034+
// 启用延时关闭,如果 linger 为 0,那么会立即关闭
1035+
// linger 最大为 65535 秒,约 18 小时
10341036
} else {
10351037
if (linger < 0) {
10361038
throw new IllegalArgumentException("invalid value for SO_LINGER");
@@ -1086,15 +1088,18 @@ public void sendUrgentData (int data) throws IOException {
10861088
* Enable/disable {@link SocketOptions#SO_OOBINLINE SO_OOBINLINE}
10871089
* (receipt of TCP urgent data)
10881090
*
1089-
* By default, this option is disabled and TCP urgent data received on a
1090-
* socket is silently discarded. If the user wishes to receive urgent data, then
1091-
* this option must be enabled. When enabled, urgent data is received
1092-
* inline with normal data.
1091+
* 默认情况下,该选项被禁用,表示接收到 TCP urgent data 时,数据将被默默丢弃
1092+
* By default, this option is disabled and TCP urgent data received on a socket is silently discarded.
1093+
* 如果希望接受 urgent data 数据,该选项必须开启
1094+
If the user wishes to receive urgent data, then this option must be enabled.
1095+
When enabled, urgent data is received inline with normal data.
10931096
* <p>
1094-
* Note, only limited support is provided for handling incoming urgent
1095-
* data. In particular, no notification of incoming urgent data is provided
1096-
* and there is no capability to distinguish between normal data and urgent
1097-
* data unless provided by a higher level protocol.
1097+
* 只提供有限的功能来处理紧急数据
1098+
* Note, only limited support is provided for handling incoming urgent data.
1099+
* 一般是不提供紧急数据的通知的,除非有更高级协议的支持
1100+
* In particular, no notification of incoming urgent data is provided
1101+
and there is no capability to distinguish between normal data and urgent
1102+
data unless provided by a higher level protocol.
10981103
*
10991104
* @param on {@code true} to enable
11001105
* {@link SocketOptions#SO_OOBINLINE SO_OOBINLINE},
@@ -1139,6 +1144,7 @@ public boolean getOOBInline() throws SocketException {
11391144
* Socket is still valid. The option <B>must</B> be enabled
11401145
* prior to entering the blocking operation to have effect. The
11411146
* timeout must be {@code > 0}.
1147+
* 设置 0 的话,就是无限超时的意思
11421148
* A timeout of zero is interpreted as an infinite timeout.
11431149
*
11441150
* @param timeout the specified timeout, in milliseconds.
@@ -1183,18 +1189,18 @@ public synchronized int getSoTimeout() throws SocketException {
11831189
* Sets the {@link SocketOptions#SO_SNDBUF SO_SNDBUF} option to the
11841190
* specified value for this {@code Socket}.
11851191
* The {@link SocketOptions#SO_SNDBUF SO_SNDBUF} option is used by the
1186-
* platform's networking code as a hint for the size to set the underlying
1187-
* network I/O buffers.
1192+
platform's networking code as a hint for the size to set the underlying
1193+
network I/O buffers.
11881194
*
11891195
* <p>Because {@link SocketOptions#SO_SNDBUF SO_SNDBUF} is a hint,
11901196
* applications that want to verify what size the buffers were set to
1191-
* should call {@link #getSendBufferSize()}.
1197+
should call {@link #getSendBufferSize()}.
11921198
*
11931199
* @exception SocketException if there is an error
1194-
* in the underlying protocol, such as a TCP error.
1200+
in the underlying protocol, such as a TCP error.
11951201
*
11961202
* @param size the size to which to set the send buffer
1197-
* size. This value must be greater than 0.
1203+
size. This value must be greater than 0.
11981204
*
11991205
* @exception IllegalArgumentException if the
12001206
* value is 0 or is negative.
@@ -1239,30 +1245,38 @@ public synchronized int getSendBufferSize() throws SocketException {
12391245
/**
12401246
* Sets the {@link SocketOptions#SO_RCVBUF SO_RCVBUF} option to the
12411247
* specified value for this {@code Socket}. The
1242-
* {@link SocketOptions#SO_RCVBUF SO_RCVBUF} option is
1243-
* used by the platform's networking code as a hint for the size to set
1244-
* the underlying network I/O buffers.
1248+
{@link SocketOptions#SO_RCVBUF SO_RCVBUF} option is
1249+
used by the platform's networking code as a hint for the size to set
1250+
the underlying network I/O buffers.
12451251
*
1246-
* <p>Increasing the receive buffer size can increase the performance of
1247-
* network I/O for high-volume connection, while decreasing it can
1248-
* help reduce the backlog of incoming data.
1252+
* 增加接受缓冲值可以提高大数据量传递时的 IO 的性能,减少时,可以帮助减少数据的积压
1253+
* Increasing the receive buffer size can increase the performance of
1254+
network I/O for high-volume connection, while decreasing it can
1255+
help reduce the backlog of incoming data.
12491256
*
12501257
* <p>Because {@link SocketOptions#SO_RCVBUF SO_RCVBUF} is a hint,
12511258
* applications that want to verify what size the buffers were set to
12521259
* should call {@link #getReceiveBufferSize()}.
12531260
*
12541261
* <p>The value of {@link SocketOptions#SO_RCVBUF SO_RCVBUF} is also used
1255-
* to set the TCP receive window that is advertized to the remote peer.
1256-
* Generally, the window size can be modified at any time when a socket is
1257-
* connected. However, if a receive window larger than 64K is required then
1258-
* this must be requested <B>before</B> the socket is connected to the
1259-
* remote peer. There are two cases to be aware of:
1262+
to set the TCP receive window that is advertized to the remote peer.
1263+
通常来说,在套接字建立连接之后,可以随意修改窗口大小
1264+
Generally, the window size can be modified at any time when a socket is
1265+
connected.
1266+
然而,当窗口大小大于 64k 时,需要注意:
1267+
1:必须在 Socket 连接客户端之前设置缓冲值
1268+
2:必须在 ServerSocket 绑定本地地址之前设置缓冲值
1269+
However, if a receive window larger than 64K is required then
1270+
this must be requested before the socket is connected to the
1271+
remote peer. There are two cases to be aware of:
12601272
* <ol>
12611273
* <li>For sockets accepted from a ServerSocket, this must be done by calling
1262-
* {@link ServerSocket#setReceiveBufferSize(int)} before the ServerSocket
1263-
* is bound to a local address.<p></li>
1264-
* <li>For client sockets, setReceiveBufferSize() must be called before
1265-
* connecting the socket to its remote peer.</li></ol>
1274+
before the ServerSocket is bound to a local address.
1275+
<p></li>
1276+
* <li>
1277+
* For client sockets, setReceiveBufferSize() must be called before
1278+
connecting the socket to its remote peer.
1279+
</li></ol>
12661280
* @param size the size to which to set the receive buffer
12671281
* size. This value must be greater than 0.
12681282
*
@@ -1420,19 +1434,21 @@ public int getTrafficClass() throws SocketException {
14201434
* Enable/disable the {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR}
14211435
* socket option.
14221436
* <p>
1423-
* When a TCP connection is closed the connection may remain
1424-
* in a timeout state for a period of time after the connection
1425-
* is closed (typically known as the {@code TIME_WAIT} state
1426-
* or {@code 2MSL} wait state).
1427-
* For applications using a well known socket address or port
1428-
* it may not be possible to bind a socket to the required
1429-
* {@code SocketAddress} if there is a connection in the
1430-
* timeout state involving the socket address or port.
1437+
套接字在关闭之后,可能会等待一段时间之后才会真正的关闭,如果此时有新的套接字前来绑定同样的地址和端口时,
1438+
如果 setReuseAddress 为 true 的话,就可以绑定成功,否则绑定失败
1439+
When a TCP connection is closed the connection may remain
1440+
in a timeout state for a period of time after the connection
1441+
is closed (typically known as the {@code TIME_WAIT} state
1442+
or {@code 2MSL} wait state).
1443+
For applications using a well known socket address or port
1444+
it may not be possible to bind a socket to the required
1445+
{@code SocketAddress} if there is a connection in the
1446+
timeout state involving the socket address or port.
14311447
* <p>
1432-
* Enabling {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR}
1433-
* prior to binding the socket using {@link #bind(SocketAddress)} allows
1434-
* the socket to be bound even though a previous connection is in a timeout
1435-
* state.
1448+
Enabling {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR}
1449+
prior to binding the socket using {@link #bind(SocketAddress)} allows
1450+
the socket to be bound even though a previous connection is in a timeout
1451+
state.
14361452
* <p>
14371453
* When a {@code Socket} is created the initial setting
14381454
* of {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is disabled.

0 commit comments

Comments
 (0)