|
52 | 52 | */
|
53 | 53 | public
|
54 | 54 | class Socket implements java.io.Closeable {
|
55 |
| - /** |
56 |
| - * Various states of this socket. |
57 |
| - */ |
| 55 | + |
| 56 | + |
58 | 57 | // 套接字的状态
|
59 | 58 | private boolean created = false;// 已创建
|
60 | 59 | private boolean bound = false;// 已绑定
|
61 | 60 | private boolean connected = false;// 已连接
|
62 | 61 | 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;// 写是否关闭 |
66 | 65 |
|
67 |
| - /** |
68 |
| - * The implementation of this Socket. |
69 |
| - */ |
70 | 66 | // 套接字的实现
|
71 | 67 | SocketImpl impl;
|
72 | 68 |
|
@@ -107,7 +103,7 @@ void setImpl() {
|
107 | 103 |
|
108 | 104 | /**
|
109 | 105 | * 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. |
111 | 107 | * <P>
|
112 | 108 | * If there is a security manager, its {@code checkConnect} method
|
113 | 109 | * is called with the proxy host address and port number
|
@@ -552,7 +548,7 @@ public void connect(SocketAddress endpoint) throws IOException {
|
552 | 548 | /**
|
553 | 549 | * Connects this socket to the server with a specified timeout value.
|
554 | 550 | * 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. |
556 | 552 | *
|
557 | 553 | * @param endpoint the {@code SocketAddress}
|
558 | 554 | * @param timeout the timeout value to be used in milliseconds.
|
@@ -1026,11 +1022,17 @@ public boolean getTcpNoDelay() throws SocketException {
|
1026 | 1022 | * @since JDK1.1
|
1027 | 1023 | * @see #getSoLinger()
|
1028 | 1024 | */
|
| 1025 | + // on 为 false,表示不启用延时关闭,true 的话表示启用延时关闭 |
| 1026 | + // linger 为延时的时间,单位秒 |
1029 | 1027 | public void setSoLinger(boolean on, int linger) throws SocketException {
|
| 1028 | + // 检查是否已经关闭 |
1030 | 1029 | if (isClosed())
|
1031 | 1030 | throw new SocketException("Socket is closed");
|
| 1031 | + // 不启用延时关闭 |
1032 | 1032 | if (!on) {
|
1033 | 1033 | getImpl().setOption(SocketOptions.SO_LINGER, new Boolean(on));
|
| 1034 | + // 启用延时关闭,如果 linger 为 0,那么会立即关闭 |
| 1035 | + // linger 最大为 65535 秒,约 18 小时 |
1034 | 1036 | } else {
|
1035 | 1037 | if (linger < 0) {
|
1036 | 1038 | throw new IllegalArgumentException("invalid value for SO_LINGER");
|
@@ -1086,15 +1088,18 @@ public void sendUrgentData (int data) throws IOException {
|
1086 | 1088 | * Enable/disable {@link SocketOptions#SO_OOBINLINE SO_OOBINLINE}
|
1087 | 1089 | * (receipt of TCP urgent data)
|
1088 | 1090 | *
|
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. |
1093 | 1096 | * <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. |
1098 | 1103 | *
|
1099 | 1104 | * @param on {@code true} to enable
|
1100 | 1105 | * {@link SocketOptions#SO_OOBINLINE SO_OOBINLINE},
|
@@ -1139,6 +1144,7 @@ public boolean getOOBInline() throws SocketException {
|
1139 | 1144 | * Socket is still valid. The option <B>must</B> be enabled
|
1140 | 1145 | * prior to entering the blocking operation to have effect. The
|
1141 | 1146 | * timeout must be {@code > 0}.
|
| 1147 | + * 设置 0 的话,就是无限超时的意思 |
1142 | 1148 | * A timeout of zero is interpreted as an infinite timeout.
|
1143 | 1149 | *
|
1144 | 1150 | * @param timeout the specified timeout, in milliseconds.
|
@@ -1183,18 +1189,18 @@ public synchronized int getSoTimeout() throws SocketException {
|
1183 | 1189 | * Sets the {@link SocketOptions#SO_SNDBUF SO_SNDBUF} option to the
|
1184 | 1190 | * specified value for this {@code Socket}.
|
1185 | 1191 | * 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. |
1188 | 1194 | *
|
1189 | 1195 | * <p>Because {@link SocketOptions#SO_SNDBUF SO_SNDBUF} is a hint,
|
1190 | 1196 | * applications that want to verify what size the buffers were set to
|
1191 |
| - * should call {@link #getSendBufferSize()}. |
| 1197 | + should call {@link #getSendBufferSize()}. |
1192 | 1198 | *
|
1193 | 1199 | * @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. |
1195 | 1201 | *
|
1196 | 1202 | * @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. |
1198 | 1204 | *
|
1199 | 1205 | * @exception IllegalArgumentException if the
|
1200 | 1206 | * value is 0 or is negative.
|
@@ -1239,30 +1245,38 @@ public synchronized int getSendBufferSize() throws SocketException {
|
1239 | 1245 | /**
|
1240 | 1246 | * Sets the {@link SocketOptions#SO_RCVBUF SO_RCVBUF} option to the
|
1241 | 1247 | * 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. |
1245 | 1251 | *
|
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. |
1249 | 1256 | *
|
1250 | 1257 | * <p>Because {@link SocketOptions#SO_RCVBUF SO_RCVBUF} is a hint,
|
1251 | 1258 | * applications that want to verify what size the buffers were set to
|
1252 | 1259 | * should call {@link #getReceiveBufferSize()}.
|
1253 | 1260 | *
|
1254 | 1261 | * <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: |
1260 | 1272 | * <ol>
|
1261 | 1273 | * <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> |
1266 | 1280 | * @param size the size to which to set the receive buffer
|
1267 | 1281 | * size. This value must be greater than 0.
|
1268 | 1282 | *
|
@@ -1420,19 +1434,21 @@ public int getTrafficClass() throws SocketException {
|
1420 | 1434 | * Enable/disable the {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR}
|
1421 | 1435 | * socket option.
|
1422 | 1436 | * <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. |
1431 | 1447 | * <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. |
1436 | 1452 | * <p>
|
1437 | 1453 | * When a {@code Socket} is created the initial setting
|
1438 | 1454 | * of {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is disabled.
|
|
0 commit comments