0

http header : Connection: Keep-Alive

After reading alot about this , I still can't understand how its working.

wiki :

A keepalive signal can also be used to indicate to Internet infrastructure that the connection should be preserved. Without a keepalive signal intermediate NAT-enabled routers can drop the connection after timeout.

I dont udnerstand :

a Server can have 1,000,000 cuncorrent connections .

John sends a request to the server.

Paul's compter is on the same lan near paul. paul also sends a request to the same server.

John's and paul organization is behind router.

enter image description here

How the hell the server knows how to keep connection alive for both paul and john ?

Also , when john sends request the second time , it "doesnt open a new conneciton" , so how does keep-alive is applied here ?

Community
  • 1
  • 1
Royi Namir
  • 144,742
  • 138
  • 468
  • 792

1 Answers1

3

First of all, TCP/IP connection is not some thin wire that is temporarily connecting two computers. At the end of the day both TCP/IP and UDP are just a series of packets. It's the operating system that pretends you have a connection by putting the IP packets back together in correct order.

Now back to your question. Note that the problem is not really HTTP-specific, all of this works on TCP/IP layer. Say Paul has 192.168.0.100 and John has 192.168.0.101 internal IP addresses while NAT has public 1.2.3.4 address. When Paul connects to some server, his OS uses 192.168.0.100:54321 address (port is chosen randomly by OS). This request hits NAT which remembers that address and forwards request to external server. The external server sees 1.2.3.4:4321 (notice the different port) as the user is behind the NAT so internal IP is not visible.

When the external server (let it be web server) sends a reply, it sends it to 1.2.3.4:4321. NAT, on the other hand, remembers that 4321 port should be forwarded to 192.168.0.100:54321` - and so it is.

Now imagine John sends request to the same server. This TCP/IP connection is routed through NAT which remember that request from 192.168.0.101:32123 was made. This request is then forwarded using public 1.2.3.4:4322 (notice different port). When response arrives, NAT checks the port if it is 4322, it routes to 192.168.0.101:32123 (John). Otherwise (on port 4321) Paul will get his reply.

Note: do not confuse client ephemeral port with server port (80 in HTTP by default).

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • Hi. Thanks. but youre actually tell me that only 65K users can use persistant connection cause `1.2.3.4:[0..65k]`....so ? – Royi Namir Sep 23 '12 at 09:13
  • @RoyiNamir: check out [Wikipedia](http://en.wikipedia.org/wiki/Network_address_translation): "*The total number of internal addresses that can be translated to one external address could theoretically be as high as 65,536 per IP address*". BTW voting to migrate, not programming related. – Tomasz Nurkiewicz Sep 23 '12 at 09:15
  • Tomas , People here are asking about data structures and memory allocations - (sequential or not and much more internal topics) - which also isnt much related to _programming_...so I disagree with you about that :-) , when I write a request - I do want to know when and how should I use the header and raise legitimacy question. – Royi Namir Sep 23 '12 at 09:21
  • when paul makes **second** request , does it still will be like the prev request ? – Royi Namir Sep 23 '12 at 12:45
  • @RoyiNamir: when Paul makes two concurrent requests, he uses two different ephemeral ports and NAT uses to different ports as well. When he makes two subsequent requests, most likely OS will assign different port to the second one. – Tomasz Nurkiewicz Sep 23 '12 at 12:49
  • According to your main answer , If it won't be persistant , what will be the difference ? will nat Wont remember where you came from ? can you spot the difference as if there **isn't** persistant connection - so I can spot the difference. Thank you very much for your help. – Royi Namir Sep 23 '12 at 12:57
  • can you please answer my lst comment ? – Royi Namir Sep 24 '12 at 06:04