File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Sockets and Internet Protocols and the fly likes How webserver knows which client to reply Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "How webserver knows which client to reply" Watch "How webserver knows which client to reply" New topic

How webserver knows which client to reply

Micheal John John

Joined: Mar 02, 2014
Posts: 4
Hi all,

I am newbie and started to learn java and servlets.
I have below query, I am running webserver in port 80.
Client A connects to the webserver and requesting some file.
Client B connects to the webserver at the same time and requesting for some other / same file.

How the webserver exactly knows how to serve the intended client? Meaning not resulted in serving wrong files to wrong client. This is for HTTP case.

How about normal TCP..instead of running webserver...writing socket I am running some standalone java pgm in special port say 905.
Client A directly connects to this port thru TCP and send some raw string for processing.
Client B also connects to this port. How will work in this case?
Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4642

Hello Micheal, welcome to CodeRanch!

In both cases it works the same, because HTTP is implemented on top of TCP/IP. The client sends a request, in the form of TCP packets. These packets contain the ip-address of the client, plus the port number. Together they form a socket. When two requests come in from different hosts, the server will simply return data to the same socket the request came from.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Jayesh A Lalwani
Saloon Keeper

Joined: Jan 17, 2008
Posts: 2749

Yeah, it's kind of a wonderful thing how this whole internet works . Ultimately, at the physical level, a given computer can only talk to the network it's physically connected to. How does it manage to communicate to a server located across the world? To understand this well, you need to understand the OSI model of networkng. I can't even begin to do justice on the subject in a post. I spent 1 semester taking an intro course on networkng in my bachelor's course, an 3 years forgetting it

If you really want to understand how networkng works, I'm afraid you will either have to go to school, or pick a textbook
Micheal John John

Joined: Mar 02, 2014
Posts: 4
Thanks a lot Stephen and Jayesh. I started to explore more on this...
I agree. Here's the link:
subject: How webserver knows which client to reply
It's not a secret anymore!