aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes How does server make a Socket connection back to the client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "How does server make a Socket connection back to the client" Watch "How does server make a Socket connection back to the client" New topic
Author

How does server make a Socket connection back to the client

Rajiv Rai
Ranch Hand

Joined: Jun 14, 2010
Posts: 57
I have been reading the "Networking and threads" chapter of the HeadFirst Java book.

A client makes a Socket connection to the server by the following code



and the server does the following



To make a Socket connection, the knowledge of the IP address and the port number is required.
I want to know how , when and where does the server get to know the IP address and port number
of the client to make a socket connection to the client?
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
The whole idea of server/client is that the server listens for, accepts and processes connections from the client. It's a one way street.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Rajiv Rai wrote:I want to know how , when and where does the server get to know the IP address and port number
of the client to make a socket connection to the client?


It doesn't make a socket connection to the client. At least, not in the code you posted. It's the client which makes a socket connection to the server.

You may have been misled by the fact that once the socket connection has been made by the client and accepted by the server, data can flow in both directions over that connection.
Ash Chan
Greenhorn

Joined: Jan 09, 2013
Posts: 3
Rajiv Rai wrote:I have been reading the "Networking and threads" chapter of the HeadFirst Java book.

A client makes a Socket connection to the server by the following code



and the server does the following



To make a Socket connection, the knowledge of the IP address and the port number is required.
I want to know how , when and where does the server get to know the IP address and port number
of the client to make a socket connection to the client?



*****************************************
To ans your Q:
A client can connect to a service once the server is ready to accept connections (accept). In order to make a connection, the client must know the IP number of the server machine and the port number of the service. If the client does not know the IP number, it needs to request name/number resolution using the function gethostbyname. Once the connection is accepted by the server, each program can communicate via input-output channels over the sockets created at both ends.


Extra info on connection establishment:
Think of an analogy:
Server ---> A restaurant
Client ---> Customer

Now the IP is like the Street name and the port number can be your door no.

The client knows this and reaches out to the server. It does this by the code ""Socket s = new Socket("127.0.0.1", 7777);"" ---> For eg if client and server are on same machine, this wud be Socket s = new Socket("LocalHost");

Little Background info: The server has 2 sockets: 'ServerSocket' to accept client connection and a 'Socket' to service the need of each client.
Now the code:
servSocket = new ServerSocket(9141);; //This is like the doorman at the restaurant who lets you in (if the client has the right port no.)

socket = servSocket.accept(); // This is like a waitress ushering the customer to the table and to service the needs of the client.



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How does server make a Socket connection back to the client