• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get the port Number  RSS feed

 
apaladugu pal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi When creating the server and client sockets how can i specify the same port in both of them
When creating the server socket i can iterate through the port numbers and assigh the available port but how can the client know the port number to connect to the server socket.Can someone please let me know how to do this
Thanks
 
Clifton Craig
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The thing is you can't dynamically figure out from a client which port a server is bound too. At least not easily or accurately. When a server is implemented it is generally up to the developer to assign a port or a way of making the port known. The client is either hard coded or soft coded to use the assigned port. For example, I once wrote a simple datagram echo server (playing with java.io and java.net) which would bind to an arbitrary port. (If memory serves I think I used port 0 on the server which means grab the next available port.) I then printed the port number bound from the server to Std out. (I used System.out.println(serverSocket.getPortNumber()) or something to that affect.) I also wote a client that would send datagram packets to a user specified port. So I would start the client (which was cmdline driven) and key the port number that I read from the console as an argument.

Unless I'm missing something (which I never do) there is no reliable way to dynaically determine a service's port. You could use a probing algorithm from the client but that's inaccurate as you never know which service is running where. Picture it like this. You have a bunch of doors (ports) on a building (computer). Behind each is a person (service) speaking a different language (protocol). You (the client) want to talk to the Puertorican guy (your service). Probing means you would open each door and say hello in his native tongue until you found the one who spoke the same language. The problem here is you could run into the Mexican lady behind door 17 who would respond "Hola". You would then assume this was the right person (service) and continue the conversation. The conversation could go on for several minutes until she says something subtle and different that would throw you off. With so many unpredictable services each with their own and sometimes similar protocols it becomes a hardship to find the right one with probing. Not to mention how long it would take going door to door. In my scenario the Puertorican guy could holler out from behind his door. But that would be the same as your server sending info to the client which could mean something like a multicast message sent over the wire. I'm getting in over my head now so I'll quit with the stoopid analogies. I hope you get the idea though.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!