Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how can client send the port number

 
sandeep dhingra
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 9279
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
sandeep dhingra
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
tim jones
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64830
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.

bear
JavaRanch Sheriff
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic