Can I bind multiple servers to the same TCP port?

会有一股神秘感。 提交于 2021-02-19 08:52:02

问题


I would expect having multiple servers on the same port would cause problems. In fact I want it to throw an exception when I try to start two servers on the same port. The problem is, it seems more than happy to start multiple servers on the same port. I can have many instances of the following code running just fine with no exceptions.

import BaseHTTPServer
import SimpleHTTPServer
import sys

def main():
    try:
        server = BaseHTTPServer.HTTPServer(('127.0.0.1',5000), SimpleHTTPServer.SimpleHTTPRequestHandler)
        print "On port: " + str(server.socket.getsockname()[1])
    except Exception, e:
        print e
    server.serve_forever()

if __name__ == "__main__":
    main()

All of which claim to be on port 5000. How can I get it to throw an exception if it tries to use a port that is already taken?

Edit: This is Python 2.6.4

Edit 2: http://www.youtube.com/watch?v=rVOG3JdbHAM&feature=youtu.be Because people seem to think what I am explaining is not possible? Or I am totally misunderstanding people. Either way, this video should clear it up. I start two servers, neither of them print out any exceptions. When I close the first, the second starts working. Why is this happening? I would expect the 2nd server to simply never start and print an exception. Is this not what should happen?


回答1:


I tried to execute your code and the second instance returned,

 [Errno 98] Address already in use

as it should. Python 2.6 on SuSE Linux.

Can check with netstat utility whether port 5000 is really taken?




回答2:


A TCP port can be bound by one and only one process. As was noted by @phihag, you only think you have multiple servers bound to port 5000 because you are throwing away the exception that would tell you that you already had bound that port.

I am running on Windows 7. It lists the ports status as TIME_WAIT

That's a secondary problem which is usually caused by clients not shutting down the connection properly. TIME_WAIT is a valid TCP state but one that will take 2 minutes for the server to clear. You should look to why your clients are dropping the connection instead of having the server close it.

If you are using a standard browser on the client side, you should assume you have a second bug in your server code that you've not shown here.

added in response to comment

There is something wrong with your expectations. This version of your code should make it clear:

def bind():
    return BaseHTTPServer.HTTPServer(('127.0.0.1',5000), SimpleHTTPServer.SimpleHTTPRequestHandler)

s1 = bind()
s2 = bind()

Where the last line will abort your program with an "Address already in use" exception. If you chose to ignore (or catch and discard) the exception, s2 will not be have a valid server. This is what the exception is telling you. In C, if you ignored errors the same way it would look like:

fp = fopen("a_file_that_does_not_exist", "r")
fgets(buf, BUFSIZ, fp)

Both fopen and fgets will return EOF which you are ignoring. The program would run, but it certainly wouldn't do anything useful because you can't read from a file that does not exist. You can ignore the error returns but holding your hands over your eyes doesn't make the monster go away either.




回答3:


I agree with the various answers and comments indicating that you cannot have multiple listeners to a single TCP port; however, I am experiencing the same behaviour as OP. I am running Windows 8, and neither instance of my program (written in C++, using Boost) has an exception thrown. The output from netstat looks like:

C:\windows\system32>netstat -an

Active Connections

  Proto  Local Address          Foreign Address        State
  TCP    0.0.0.0:135            0.0.0.0:0              LISTENING
  TCP    0.0.0.0:445            0.0.0.0:0              LISTENING
  TCP    0.0.0.0:5357           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:7112           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:17500          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49152          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49153          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49154          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49155          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49157          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:50102          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:50102          0.0.0.0:0              LISTENING
  TCP    127.0.0.1:2559         0.0.0.0:0              LISTENING
  TCP    127.0.0.1:19872        127.0.0.1:49248        ESTABLISHED
  TCP    127.0.0.1:49248        127.0.0.1:19872        ESTABLISHED
  TCP    127.0.0.1:50102        127.0.0.1:52616        TIME_WAIT
  TCP    127.0.0.1:50102        127.0.0.1:52618        TIME_WAIT
  TCP    127.0.0.1:50102        127.0.0.1:52620        TIME_WAIT
  TCP    127.0.0.1:50102        127.0.0.1:52622        TIME_WAIT

In this case, the first program instance to start listening to port 50102 receives all the client connections. Then, when I close that instance, the second instance starts receiving the client connections. Strange but true.

I was hoping that the second program instance would be unable to start listening to the same port as the first instance.



来源:https://stackoverflow.com/questions/11570290/can-i-bind-multiple-servers-to-the-same-tcp-port

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!