Can two applications on the same machine bind to the same port and IP address? Taking it a step further, can one app listen to requests coming from a certain IP and the other to another remote IP? I know I can have one application that starts off two threads (or forks) to have similar behavior, but can two applications that have nothing in common do the same?
相关问题
- Multiple sockets for clients to connect to
- Drakma and Dexador both fails at USocket call whil
- difference between file descriptor and socket file
- Port of the Rails app when running Cucumber tests
- Can we create a Silverlight Socket Server ONLY usi
相关文章
- Socket编程 TCP方式发送时间有点长
- socket() returns 0 in C client server application
- Passing extra metadata to a RequestHandler using p
- How do I get the external IP of a socket in Python
- Native hooking in Android Client
- Is zeroing out the “sockaddr_in” structure necessa
- How many times will TCP retransmit
- Does the TCPServer + BaseRequestHandler in Python&
In principle, no.
It's not written in stone; but it's the way all APIs are written: the app opens a port, gets a handle to it, and the OS notifies it (via that handle) when a client connection (or a packet in UDP case) arrives.
If the OS allowed two apps to open the same port, how would it know which one to notify?
But... there are ways around it:
Yes Definitely. As far as i remember From kernel version 3.9 (Not sure on the version) onwards support for the
SO_REUSEPORT
was introduced.SO_RESUEPORT
allows binding to the exact same port and address, As long as the first server sets this option before binding its socket.It works for both TCP and UDP. Refer to the link for more details: SO_REUSEPORT
Note: Accepted answer no longer holds true as per my opinion.
You can have one application listening on one port for one network interface. Therefore you could have:
httpd
listening on remotely accessible interface, e.g.192.168.1.1:80
127.0.0.1:80
Sample use case could be to use
httpd
as a load balancer or a proxy.Yes.
Multiple listening TCP sockets, all bound to the same port, can co-exist, provided they are all bound to different local IP addresses. Clients can connect to whichever one they need to. This excludes
0.0.0.0
(INADDR_ANY
).Multiple accepted sockets can co-exist, all accepted from the same listening socket, all showing the same local port number as the listening socket.
Multiple UDP sockets all bound to the same port can all co-exist provided either the same condition as at (1) or they have all had the
SO_REUSEADDR
option set before binding.TCP ports and UDP ports occupy different namespaces, so the use of a port for TCP does not preclude its use for UDP, and vice versa.
Reference: Stevens & Wright, TCP/IP Illustrated, Volume II.