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.
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
Joined: Jul 30, 2005
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.
Joined: Jan 29, 2003
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.
Joined: Jul 30, 2005
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. thanks.
hii, 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.
"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.