@param s String address, like 10.132.12.54:4312 @return null if the string s is not a valid address
(String s, int defaultPort)
| 43 | * @return null if the string s is not a valid address |
| 44 | */ |
| 45 | public static netadr_t fromString(String s, int defaultPort) { |
| 46 | if (s.equalsIgnoreCase("localhost") || s.equalsIgnoreCase("loopback")) { |
| 47 | return net_local_adr; |
| 48 | } else { |
| 49 | try { |
| 50 | netadr_t result = new netadr_t(); |
| 51 | String[] address = s.split(":"); |
| 52 | InetAddress ia = InetAddress.getByName(address[0]); |
| 53 | result.ip = ia.getAddress(); |
| 54 | result.type = NetAddrType.NA_IP; |
| 55 | if (address.length == 2) |
| 56 | result.port = Lib.atoi(address[1]); |
| 57 | if (result.port == 0) |
| 58 | result.port = defaultPort; |
| 59 | return result; |
| 60 | } catch (Exception e) { |
| 61 | Com.Println(e.getMessage()); |
| 62 | return null; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public netadr_t() { |
| 68 | this.type = NetAddrType.NA_LOOPBACK; |
no test coverage detected