File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How does server make a Socket connection back to the client

 
Rajiv Rai
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 187
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20193
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic