Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Port to Service Name in Java?

My services file (C:\WINDOWS\system32\drivers\etc\services) has a bunch of Port to Service mappings:

echo                7/tcp
echo                7/udp
discard             9/tcp    sink null
discard             9/udp    sink null
systat             11/tcp    users                  #Active users
systat             11/udp    users                  #Active users
daytime            13/tcp
daytime            13/udp
qotd               17/tcp    quote                  #Quote of the day
qotd               17/udp    quote                  #Quote of the day
chargen            19/tcp    ttytst source          #Character generator

I am trying to find a way to convert from a Port to the Service Name programmatically through Java APIs (instead of parsing) or third party libraries?

Pseudocode:

Port port = new Port("443","tcp");
String service = port.getService();
System.out.println(service);  //prints "https"

Is there any good way to accomplish this?

like image 605
mainstringargs Avatar asked Aug 03 '10 01:08

mainstringargs


1 Answers

What you are looking for is a Java implementation of the Linux getservbyport() system call. Take a look at http://github.com/wmeissner/jnr-netdb. Also, do a Google search for java getservbyport

like image 149
Jim Garrison Avatar answered Sep 24 '22 16:09

Jim Garrison