tags:

views:

461

answers:

7

When I try to bind socket to port 80 from program I get error, but how two browsers simultaneously could listen to same port 80 ?

+16  A: 

They don't listen on port 80 they talk to port 80, or 443 if you're using SSL (or on any other port if the admin broke with convention, you may have seen urls like http://www.site.com:8080 where the site has been set up on port 8080).

The browser will make the request from a random high-numbered port so more than browser can be active at the same time.

As paxdiablo says, you can use netstat to see what programs are listening for connections (using "netstat -a -b" will show which executable is bound to which port)

wefwfwefwe
+4  A: 

The browsers aren't actually bound to port 80 at all. You will probably find that you are also running IIS or another web server that is bound and that is the cause of your problems

ZombieSheep
+7  A: 

Browsers actually don't listen on port 80. The web-servers do this and the browser opens a connection with a port between 49152 and 65535 I think (dynamic ports).

+4  A: 

In a network connection there is one peer (usually called client) that connects to another (usually called a server). The server is said to be listening on a certain port, while the client is said to be connecting to that port.

In this case the web server listens on port 80 while all clients (browsers) connect to it.

Andreas Bonini
+1  A: 

When a browser makes a connection to a server, it binds to a local non-priviledged port, but connects to the web server's port 80. When the server sends back a response, it goes to the non-priviledged port on the browser's computer. If there are two browsers on the same computer, they bind to different ports.

Paul Tomblin
+3  A: 

Browsers do not listen on port 80, HTTP servers do (although that's just convention, you could easily have an FTP or telnet server using port 80).

In TCP/IP, a "session" must be unique and the session is defined as the 5-tuple (protocol, sourceIP, destinationIP, sourcePort, destinationPort). This allows the packets to be routed correctly on the internet.

Typically when a client attempts to contact a server, it specifies 0 as its source port which means that the operating system assigns it an unused one. That means that the client will actually listen on that port, not port 80.

So you may get a session with the properties (TCP,mybox.com,1101,www.microsoft.com,80) when your browser goes out to access Microsoft's web pages.

If you find you cannot bind your server to port 80, it will most likely because you already have a server running on that port, or your program doesn't have the required privileges to bind to that port (ports less than 1024 are generally considered privileged ports).

Running netstat -a (on Linux or Windows) will tell you whether a server is bound to port 80. Look for a listener on port 80 (or http if it's resolving ports to service names), something like:

tcp  0  0  localhost:http  *:*  LISTEN
paxdiablo
A: 

If you mean "how can two servers listen on port 80", consider using the HTTP Server API. Both servers can register a callback function, together with a URL. The client can then decide which server to call based on the URL, f.e.

http://localhost/Service1 would go to the service that has specified "http://localhost/Service1" as its URL.

Pim