~/ learn/ comp-348/ cards/ Addresses, ports, and names
1 of 5

Derive the (address, port) pair a socket needs from a URL

Derive the (address, port) pair a socket needs from a URL

Answer

URL page = new URL("http://www.athabascau.ca/calendar/index.html"); int port = page.getPort() == -1 ? page.getDefaultPort() : page.getPort(); InetAddress host = InetAddress.getByName(page.getHost()); // one DNS round trip System.out.println(host.getHostAddress() + ":" + port);

Neither half of the pair was written in the URL. The port came from the scheme because `getPort()` returns -1 when the text did not state one, and the address came from a name lookup that talked to a DNS server. Print `host` and you have proof the round trip happened: a dotted quad the URL never contained.

Harold 4e ch1 §IP Addresses and Domain Names; JDK javadoc java.net.URL

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/19fddc6a-fd23-4044-b863-0aaa2e31a033/flashcard utf-8 LF