• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How can I get ServerSocket port number?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using TCP protocol for my client. How can I get the port number of the ServerSocket to create the client socket if I only know the server's hostName and/or InetAddress. I can get the port address of the server in c with the function 'getservbyname', is there a java equivalent method? or is there another way?
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the c function for RPC?
I don't think there could be such a function, unless there is some naming service, which is another server running on another port. So the client would need to know the port of that naming service or the naming service may be running on a well-known port known to all clients.
In Java you have RMI with which you can locate a server object by name. Although you don't need to know the port of RMI Server you may need to know port of RMI Registry- though by default RMI Registry runs on port 1099.
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are missing something fundamental here. A server machine may have many server programs each listening on a different port. The client must know the port # of the server program. IP/Port fully qualifies the server program. They do have name services for remote objects, but that's at a higher level than sockets.
 
Alan Berg
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Linux the c function getservbyname is in the netdb.h header file.
#include <netdb.h>
struct servent *getservent(void);
struct servent *getservbyname(const char *name, const char *proto);
struct servent *getservbyport(int port, const char *proto);
void setservent(int stayopen);
void endservent(void);
DESCRIPTION
The getservbyname() function returns a servent
structure for the line from /etc/services that matches
the service name using protocol proto.
The servent structure is defined in <netdb.h> as follows:
struct servent {
char *s_name; /* official service name */
char **s_aliases; /* alias list */
int s_port; /* port number */
char *s_proto; /* protocol to use */
}
 
Mark Savory
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suspect that, since C and unix were developed hand-in-hand, that C would provide this platform dependent method that Java would not. I have an idea: Write a class that reads a properties file that provides the mapping between a service name and the IP/Port information. Your client can then use this properties class to get the IP/Port and create a socket connection.
 
Rahul Rathore
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alan, the fact remains that from a client host, you cannot directly retrieve the port of server program running on a remote host, merely from a 'name'. You must either know the port of the server program (in cases of FTP/HTTP these ports are well-known), OR you must connect to a naming service which may run on a well-known port OR you may make your own custom naming service as Mark says. Your custom naming service can also take advantage of existing protocols, eg. you may define an HTTPServlet (port 80), which returns you the number of the port from the name supplied by the client.
RPC has a dynamic port mapping service called portmapper which runs on the well-known port 111. The port mapper knows the mapping between server programs and ports on which they are running. The remote clients connect to port mapper running on port 111 to find out the port of the target server programs.
If you want to call the c-function getservbyname from a remote-client, then this it has to be accessed through a server program which runs on a known port. In fact the c-function MUST be part of a naming-service provided by Linux or RPC. Maybe things would become clearer if you could give a sample program showing how a remote client is accessing this function.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote a method to search the /etc/services file
(or /etc/inet/services or c:\winnt\system32\drivers\etc\services).


It is at
<A HREF="http://www.dan.drydog.com/java/GetServiceByName.java</a rel="nofollow">" TARGET=_blank>http://www.dan.drydog.com/java/GetServiceByName.java


This isn't a part of Java because someone apparently thought
it's a system-dependent function.
With all due respect, this is not a system-dependent
function. The implementation may be (such as the location of
the /etc/services file or whether it's a flat file or database),
but the function can be implemented on any system.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear oh dear.
That reply from Mark Savory is utter coblers.
How is an API platform specific !
Rubbish.
How is looking up a service by name any different to looking up a host by name. In each case the client has a symbolic name and also in each case the client uses an api to connect to a local (eg file) or network (eg DNS or NIS) resource to tranlate the name to ip address or port number.
There are so many complete Java heads out there whose only experience is the JDK and a few other services. They know so little but are quite happy to sound off.

One Java'esque way to emulate the very handy netdb functions would be to define an interface that looks much like the netdb api for getservbyname() etc and then implement this interface with a class that either reads a config file (eg on unix /etc/services) and/or uses JNDI with the NIS adapter to contact NIS.
I've got a getservbyname() implementation that uses a NIS - I'll dig it out.
I have seen so any Java networking program that hard code port numbers despite the fact that they operate in an environment where programs written in perl / c / c++ etc use netdb in order to be more flexible. Very sad but simply a symptom of the narrow spectrum of experience of so many Java 'programers'.
[ July 03, 2003: Message edited by: JL Junk ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ouch
reply
    Bookmark Topic Watch Topic
  • New Topic