Port to Service Name in Java?

随声附和 提交于 2019-12-18 06:09:19

问题


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?


回答1:


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




回答2:


I don't think there is some API in java for this purpose. You will either need to parse this file or maintain port service mapping in some form (Properties file,db etc.). example to parse from system file is :

http://www.javafaq.nu/java-example-code-162.html



来源:https://stackoverflow.com/questions/3393012/port-to-service-name-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!