CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2010
    Posts
    1

    How can multiple servers be listening to the same HTTP port 80?

    Hi,
    I'm not a newbie to socket programming, but then again I'm not even close to an expert!

    I have a conceptual question.
    Suppose to bypass firewalls you make your client server app run on port 80 (ie the server is listening on port 80). Now, if this chat server is listening on port 80 for incoming connections, would it be possible for it to also serve as an HTTP server (webserver), serving webpages even though the web server would also be listening to port 80?

    The idea beats me, but I've seen it work! Shouldn't this cause any serialization issues? Or how would the servers even find out, which data is meant for which application...

    Any explanations?

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: How can multiple servers be listening to the same HTTP port 80?

    The only way I can think of that that could be possible is if both servers are listening on different interfaces (i.e. 2 ethernet cards are installed) but this would require setting up the servers in a way not normally exposed to an application's configuration (most apps just listen on INADDR_ANY). Are you positive both are listening on the same port?

  3. #3
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: How can multiple servers be listening to the same HTTP port 80?

    A generalized server could be listening on port 80 an try to determine what kind of request it gets on first data arrival, and subsequently dispatch the data to either the web server or the chat server, depending on the sender's IP address. Other than that, there is no chance that 2 server reliably listen on the same IP address and port.

  4. #4
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: How can multiple servers be listening to the same HTTP port 80?

    Multiple servers cannot listen on the same port at the same time. If a second server (e.g., your web server) calls listen() on a port already occupied by another server (e.g., your chat server), then the call to listen() will fail with GetLastError() == WSAEADDRINUSE

    There might be exceptions based on SO_REUSEADDR but that's a relatively advanced topic.

    Mike

  5. #5
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: How can multiple servers be listening to the same HTTP port 80?

    The use of SO_REUSEADDR is only valid for servers when they startup. It makes sure a server can use the well-known port after e. g. a crash. Otherwise, the socket might be in state TIME_WAIT and the server would be unable to restart.

    Using SO_REUSEADDR in other cases, i. e. for "sharing" a port between 2 servers, will lead to undefined behaviour. The socket is kind of "stolen" from the first server when the scond server sets this option.

    I have made some experiments on a Windows XP some years ago, and the result was unpredictable. I strongly discourage the use for other purposes than the one mentioned above.

  6. #6
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: How can multiple servers be listening to the same HTTP port 80?

    Agree completely with Richard.J

    In fact, SO_REUSEADDR raises a security issue that MS addressed in a newer option called SO_EXCLUSIVEADDRUSE, as described here: "Using SO_REUSEADDR and SO_EXCLUSIVEADDRUSE" at http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured