1

If I, on OS X,

  1. Start Firefox.
  2. Start Google Chrome.
  3. Start a Ruby on Rails server on port 80 (using RVM)

    rvmsudo rails -p 80
    
  4. Use lsof to look at what is running on port 80

    sudo lsof -i:80
    

Then I see "ruby", "firefox", and "Google" in the output.

I hear all the time that only one process can listen on a port. How can all of these use port 80 at the same time? How is the way Firefox and Chrome are using the port different from the way Rails is using the port?

3 Answers3

1

After learning about and trying sudo lsof -i and sudo lsof -iTCP, and reading "Do web browsers use different outgoing ports for different tabs?", I think I'm just reading the output from lsof incorrectly.

Here's an example:

$ sudo lsof -iTCP:80
COMMAND  PID  USER      FD  TYPE     DEVICE  SIZE/OFF  NODE NAME
...
firefox xxxx  user  xxxxxx  IPv4  xxxxxxxxx    xxxxxx   TCP 192.168.0.100:12345->stackoverflow.com:http (ESTABLISHED)

Looking at this example and other output from sudo lsof -i I think it's pretty clear that this line from Firefox appears in the output because Firefox is connecting to port 80 on a remote server, from the local port 12345. Firefox is not connecting to a remote server from the local port 80.

I don't know if it is possible for a client like Firefox to connect from port 80 while a local server is listening on port 80, but what is happening here at least doesn't seem to challenge that idea, because that is not what Firefox is doing.

sudo lsof -i:80 -s TCP:^LISTEN shows the connections to port 80 on remote computers, unless a local client uses local port 80 to connect to something. It's strange there doesn't seem to be an option to just list which local ports are in use.

0

Generally all browsers ( client side ) connecting to port 80 on web server ( server side ). So at the server side only port 80 is on listening state. Browsers use any TCP connection to port 80 on web server.

BDRSuite
  • 6,378
-1

Short answer: a port cannot be shared

As you only have one IP and you may be recieving a lot of data coming to you at the same exact time, ports are being used to send the data to the process meant to recieve it. This lead us to the first line: ports cannot be shared.

Server side processes have an specific port and they never change it unless you do so but, in the other hand, the client just opens a random high port and connects to the server. That port may be used again but the same process again or not, but you never know and it doesn't matter.

sysfiend
  • 527
  • 1
  • 6
  • 15