Package dev.brachtendorf
Class NetworkUtil
- java.lang.Object
-
- dev.brachtendorf.NetworkUtil
-
public class NetworkUtil extends Object
-
-
Constructor Summary
Constructors Constructor Description NetworkUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringcollectSocket(Socket s)Read all the content available to be read by the socket and return the content as a String.static StringcollectSocket(Socket s, int msTimeout)Read all the content available to be read by the socket and return the content as a String.static StringcollectSocketAndClose(Socket s)Read all the content available to be read by the socket and return the content as a String.static StringdumpReader(BufferedReader br)Collect all content available in the reader and return it as a stringstatic StringreadLineFromURL(String url)Read a single line from a given urlstatic InetAddressresolvePublicAddress()Resolves the public facing ip address of this network.static InetAddressresolveSiteLocalAddress()Resolves the first found link local address of the current machine 192.168.xxx.xxx.
-
-
-
Method Detail
-
resolveSiteLocalAddress
public static InetAddress resolveSiteLocalAddress() throws IOException
Resolves the first found link local address of the current machine 192.168.xxx.xxx. This is a fix forInetAddress.getLocalHost().getHostAddress();which in some cases will resolve to a loopback address (127.0.01).Site local addresses 192.168.xxx.xxx are available inside the same network. Same counts for 10.xxx.xxx.xxx addresses, and 172.16.xxx.xxx through 172.31.xxx.xxx
Link local addresses 169.254.xxx.xxx are for a single network segment
Addresses in the range 224.xxx.xxx.xxx through 239.xxx.xxx.xxx are multicast addresses.
Broadcast address 255.255.255.255.
Loopback addresses 127.xxx.xxx.xxx
- Returns:
- link local address of the machine or InetAddress.getLocalHost() if no address can be found.
- Throws:
IOException- if address can not be resolved- Since:
- 1.0.0 com.github.kilianB
-
resolvePublicAddress
public static InetAddress resolvePublicAddress() throws IOException
Resolves the public facing ip address of this network. The public ip address is not the ip address assigned by the router, but rather the ip address exposed during web requests. External hosts trying to access this device will use this ip.- Returns:
- the public ip address of this network.
- Throws:
IOException- if the ip can not be resolved- Since:
- 1.5.9 com.github.kilianB
-
readLineFromURL
public static String readLineFromURL(String url) throws IOException
Read a single line from a given url- Parameters:
url- the url to read a line from- Returns:
- the content of the first line as returned by the url
- Throws:
IOException- if an io error occurs- Since:
- 1.5.9 com.github.kilianB, 1.5.10 public com.github.kilianB
-
dumpReader
public static String dumpReader(BufferedReader br) throws IOException
Collect all content available in the reader and return it as a string- Parameters:
br- Buffered Reader input source- Returns:
- Content of the reader as string
- Throws:
IOException- Exception thrown during read operation.- Since:
- 1.0.0 com.github.kilianB
-
collectSocketAndClose
public static String collectSocketAndClose(Socket s) throws IOException
Read all the content available to be read by the socket and return the content as a String. The socket is closed after reading is done.- Parameters:
s- The socket- Returns:
- the content of the sockets input stream as string
- Throws:
IOException- if an IOError occurs- Since:
- 1.0.0 com.github.kilianB
-
collectSocket
public static String collectSocket(Socket s) throws IOException
Read all the content available to be read by the socket and return the content as a String.- Parameters:
s- The socket- Returns:
- the content of the sockets input stream as string
- Throws:
IOException- if an IOError occurs- Since:
- 1.0.0 com.github.kilianB
-
collectSocket
public static String collectSocket(Socket s, int msTimeout) throws IOException
Read all the content available to be read by the socket and return the content as a String.- Parameters:
s- The socketmsTimeout- the time elapsed after the last character is read.- Returns:
- the content of the sockets input stream as string
- Throws:
IOException- if an IOError occurs- Since:
- 1.0.0 com.github.kilianB
-
-