This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How does server make a Socket connection back to the client

 
Ranch Hand
Posts: 57
  • 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?
 
Ranch Hand
Posts: 187
  • 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.
 
Marshal
Posts: 25815
69
Eclipse IDE Firefox Browser MySQL Database
  • 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.
 
Greenhorn
Posts: 3
  • 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.



 
If you send is by car it's a shipment, but if by ship it's cargo. This tiny ad told me:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic