Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Tony Petroski
Tony Petroski
1,789 Points

localhost remote_ports

Hi All

I'm trying to improve my understanding around ports. Correct me if wrong, browsers work with http port 80(listen to -> incoming communications). Now if you run an apache server you also set this to 80(listen to -> incoming communications). However, when i run the $_SERVER['remote_port'] i get a port number of:

'HTTP_HOST' => string(9) "localhost"

string(9) "localhost" 'SERVER_ADDR' => string(3) "::1"

'SERVER_PORT' => string(2) "80"

'REMOTE_PORT' => string(5) "60803"

What is this remote_port number? is it the communication out port from the browser IP:PORT number attached?

Tony.

2 Answers

You are correct, it is the 'remote' or 'client' machine's port the server is responding to. If it is a local(host) server, it is physically the same machine, but we can also say it is the client port in the client-server model. Your server is listening on port 80, but when the client makes the request it uses a random (generally non well-known) port to bring the response back in from the server (in this case it somewhat arbitrarily chose 60803, next time, who knows?).

for instance: I just ran: netstat -n on the Windows machine I am currently logged in on and can see that I (as a client) am connected on various ports (mostly in the range of 25000-27000, among others) to a few different web servers listening on port http port 80 and https port 443.

Hope this helps!

Tony Petroski
Tony Petroski
1,789 Points

Thanks James.

Let's try this scenario for my clarity:

Client Browser listening port #80. Server Listening port #5250.

Client Dynamic port generation #32567

Transmit packet from browser to server Local 200.100.010.001: 32567

Server 278.132.012.080: 525

Now when the server responds back to the browser, does it return to..

200.100.010.001: 32567

OR

would the packet go to the browser listening port 200.100.010.001:80 ?

That's what is confusing me.

TCP/IP is a giant rabbit hole sometimes. we'll set up an example scenario: Server details: IP: 2.2.2.2 Port: 80 (typical for a web server)

Client details: IP: 5.5.5.5 Port: Randomly-generated (we'll say... 32958)

TL;DR: Client sends request out 5.5.5.5:32958 ------to server-----------> 2.2.2.2:80 Server replies back: 5.5.5.5:32958<-------from server--------2.2.2.2:80

Client requests web page by sending a request to 2.2.2.2:80. When the client sends the request, it will generally be expecting a response, so it opens up a random port and temporarily listens on that port for incoming replies. When it sends the initial request, the port it is using to send from is included (this is built in to the UDP/TCP) in the packet headers so when the packet(s) arrive at the server, the server knows where to send the reply traffic (in this case replies will be sent to IP address 5.5.5.5 and port 32958. Then comes port-overloaded NAT for IPv4 networks...