How does a web server return an HTTP request?

5,429

Solution 1

The packet has a source address as mentioned earlier, but also has a port number assigned. This is especially needed in a NAT environment (where the internal IPs are private and external IP is constant - like sharing the internet connection). The router assigns a random unused port number and keeps a table of who the port number refers to (the user's computer). When the packet is received by the router, it looks at the port number and matches and sends to the computer/user.

feel free to update/correct this if found wrong, but that is the how it works. Hope this helps.

Solution 2

The web server's response is handled just like the client's request. They're just IP datagrams.

The web server has a default gateway that it sends its response to. The response "hops through the internet and is eventually received" by the client that originated the request.

The web server "knows" nothing about the requesting client's router.

Edit:

Your comment betrays an assumption that I made.

You're talking about a scenario where the requesting client computer has a private IP address and is attached to a network with a Network Address Translation (NAT) router at the border of the client's network (where that network connects to the Internet).

In that case, the client computer's IP address will be translated, by the NAT router, into the a public IP address assigned to the NAT router's "external" interface. The NAT router keeps track of the request such that the response is routed back to the client when it is received from the Internet.

Share:
5,429

Related videos on Youtube

ejk314
Author by

ejk314

I'm a Software Engineer and Mathematician set to graduate with his Masters in SE by May 2015.

Updated on September 18, 2022

Comments

  • ejk314
    ejk314 over 1 year

    I'm sorry if this is a really stupid question; my networking knowledge is pretty sparse.

    So, a user makes a HTTP request to some address. That request comes from an address on a local subnet, but the router knows to send the request to its default gateway because the destination IP doesn't match anything else in its routing table. The request hops through the internet and is eventually received by the destination machine (a web server). That all makes sense.

    But now, how does the web server manage to send the request back to the local machine? Didn't the request come from an IP address on a local subnet? Where did it pick up the IP address of the local machine's router? And even if it has that router's IP, how does the packet get routed to the local machine once it gets to the router?

    • HBruijn
      HBruijn over 9 years
      Each TCP/IP packet includes the source ip-address of your users PC. The web server response is routed to that IP-address the same method the request made it to the webserver (although not necessarily along the same route).
    • ejk314
      ejk314 over 9 years
      But the source IP of my local machine is some local IP address, like 192.168.0.11/24. The router can't just send a packet to that address, can it?
    • HBruijn
      HBruijn over 9 years
      If you read the article Source address This field is the IPv4 address of the sender of the packet. Note that this address may be changed in transit by a network address translation device. That may be changed should be considered as: is usually changed, as most consumers are indeed behind NAT devices. The NAT device maintains a translation table incoming packet at port X should go to internal address 192.168.0.11 port Y.
    • ejk314
      ejk314 over 9 years
      Thanks. Network Address Translation - I've never heard of that. That's probably why I'm so confused. I'll read up on it.
    • underscore_d
      underscore_d over 6 years
  • ejk314
    ejk314 over 9 years
    Clearly, there's some terrible preconceived ignorance here on my part - I'm trying to figure out what that is. The client's IP address is something like 192.168.0.11/24, so how can the packet get sent to that address? I couldn't just send off a request to some arbitrary address on a different subnet my router didn't know about, right?
  • HBruijn
    HBruijn over 9 years
    Your router is a network translation device which modifies the source address in each and every packet and updates with its public ip-address. It also maintains a translation table so that reponse packets coming in on that public IP-address are routed to the correct internal IP-address.
  • AFH
    AFH over 9 years
    I have just seen HBruijn's comment below, which gives summarises the process quite well without using technical jargon.