

How does Java ServerSocket get a new socket bound to the same local port after a...
source link: https://www.codesd.com/item/how-does-java-serversocket-get-a-new-socket-bound-to-the-same-local-port-after-accepting-the-client.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How does Java ServerSocket get a new socket bound to the same local port after accepting the client?
I am confused about Socket and ServerSocket port usages. Oracle's java tutorial about sockets says the following :
What Is a Socket?
Normally, a server runs on a specific computer and has a socket that is bound to a specific port number. The server just waits, listening to the socket for a client to make a connection request. On the client-side: The client knows the hostname of the machine on which the server is running and the port number on which the server is listening. To make a connection request, the client tries to rendezvous with the server on the server's machine and port. The client also needs to identify itself to the server so it binds to a local port number that it will use during this connection. This is usually assigned by the system.
If everything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bound to the same local port and also has its remote endpoint set to the address and port of the client. It needs a new socket so that it can continue to listen to the original socket for connection requests while tending to the needs of the connected client.
On the client side, if the connection is accepted, a socket is successfully created and the client can use the socket to communicate with the server. The client and server can now communicate by writing to or reading from their sockets.
I tried the following code to test. But it throws an exception.
try {
ServerSocket serverSocket = new ServerSocket(8080);
Socket socket = serverSocket.accept();
// This prints 8080
System.out.println("Local port of accepted socket : " + socket.getLocalPort());
// But this throws java.net.BindException: Address already in use: JVM_Bind
Socket myClientSocket = new Socket("www.google.com", 80, null, 8080);
} catch (Exception e) {
e.printStackTrace();
}
My question is obvious. While the socket returned from serverSocket.accept()
can use the same local port (8080
) why the socket that I created cannot use it?
To keep it simple, the listening TCP socket is already explicitly bound to the port, so you can't explicitly bind a second TCP socket to the same port (unless both sockets are also explicitly bound to different IP addresses, not including INADDR_ANY).
The accepted sockets don't go through an explicit 'bind' process. They acquire their local IP address and port automagically, as does an outbound socket if you connect it without binding.
Recommend
-
87
本文主要从源代码的角度来分析Jetty的Connector如何通过ServerSocket来绑定和监听网络地址和端口的过程。 Jetty的Connector实现类是ServerConnector,循例从它的doStart()方法开始(至于是如何到达这个方法,请移步本系列的前几篇): 它的doStart方法先调用父类Abs...
-
88
In an earlier post, I asked how fast the getline function in C++ could run through the lines in a text file. The answer was about 2 GB/s. That is slower than some of the best disk drives and network connections. If you t...
-
13
Talking to Postgres Through Java 16 Unix-Domain Socket Channels Posted at Jan 31, 2021 Update Feb 5: This post is disc...
-
12
Armin's BlogJAVA Socket编程:编写即时通讯系统March 28, 2016 发送文件、表情、信息 多人同时聊天 离线发送消息(上线后就...
-
13
JAVA Socket编程:使用HTTP实现网络通信March 22, 2016 服务器功能 提供 HTML,JPG 等 MIME 类型的资源。 客户端功能 访问服务器,获取 HTML 和 JPG 资...
-
7
JAVA Socket编程:使用FTP实现网络通信March 12, 2016 FTP通信流程和通信命令 FTP使用2个端口,一个数据端口和一个命令端口(也叫做控制端口)。这两个端口一般是21(命令端口)...
-
10
Branch And Bound—Why Does It Work? December 19, 2012 One of the mysteries of computational theory George Nemhauser is one of the world experts on all things havi...
-
6
Yosemite - MAMP - Unable to connect to local MySQL server via socket '/Applications/MAMP/tmp/mysql/mysql.sock' (2) advertisements I have probl...
-
4
Enter key does not work after encoding with java advertisements i get a trouble when i write this code for a jB...
-
9
Wei Sun 20 hours ago nodejs fails to connects to local hana cloud with hana-client library: Socket closed by peer ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK