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 can client send the port number 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 can client send the port number" Watch "how can client send the port number" New topic

how can client send the port number

sandeep dhingra
Ranch Hand

Joined: Jul 30, 2005
Posts: 41
hi all,
i want to know that how can a client send the port number on which it is listening to the server so that the server sends data on that port number. the server here handles many clients at a time.
Joe Ess

Joined: Oct 29, 2001
Posts: 9190

I'm curious as to why you would do such a thing. Sockets are two-way so you can send data from the server to the client with the same connection you made client to server.

[How To Ask Questions On JavaRanch]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
We could imagine a case where there are thousands of clients, and opening and holding connections from all clients to the server would chew up too many server resources. Then we could do something like the following. Because the machines in this setup reverse client and server roles, I'm going to call them workstations and a hub.

First, when workstations join the application, they open a serversocket and register that serversocket address with the hub. The hub stores all the registered workstation addresses.

Then if we want to send a message from the hub to all the workstations, we can get the list of addresses from storage and for each workstation open a connection and send the message.

If you wanted to send the message to select workstations and not all of them, the registration might include attributes that help you decide which ones to send to. Maybe what state the workstation lives in or what topics the user is interested in.

This avoids having open connections all day long, but adds the time and complexity of opening a new connection every time you send a message. If you can afford the open connections all day long, they are two-way and you wouldn't need all this.
[ February 19, 2007: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
sandeep dhingra
Ranch Hand

Joined: Jul 30, 2005
Posts: 41
Thank you stan.
What i want to do is exactly what you have written.
Can you help me with a little bit of code snippet of both the server and client side as to how the server socket will work on the client side and send it's port number.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You can make up your own "protocol" ... rules for what gets sent back and forth. The simplest thing is to just use Strings. The workstation might send "REGISTER address" or "DEREGISTER address" at logoff. The hub might send "MESSAGE text". That's all up to you.

Have you run through the Sun Network Tutorial? It does a great job of introducing messaging over sockets and shows how custom protocols can be as simple as the three keywords I used above.

BTW: What is your client? If it's a browser, you can run the client side code in an applet, but that's not an ideal thing in the open world. My project does exactly that with captive corporate users where we control installation of the JRE and everything. If it's a fat client, you'll have far fewer problems.
sandeep dhingra
Ranch Hand

Joined: Jul 30, 2005
Posts: 41
Yes, Stan my client is an applet, which is showing data to the user from the database through the server.
I have to make the server so that it can handle multiple clients and store information about all the clients. The data has to be shown by querying the database every 15 seconds.
I will just look at the tutorial you mentioned.
If you can help me a bit more it would be great.
tim jones

Joined: Feb 09, 2007
Posts: 26
Thanks Stan, I was having the same problem and your post was of immense help to me. But can you help us out by giving an example code snippet of passing the the machine name and port number to be stored.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63870

"udhaya t", you have previously been warned on one or more occasions regarding adjusting your display name to meet JavaRanch standards. This is not optional. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it prior to your next post.

Your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Be aware that accounts with invalid display names are removed.

JavaRanch Sheriff

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: how can client send the port number
It's not a secret anymore!