YouTip LogoYouTip

Net Connected

# Java Example - Connect to a Specified Host Using Socket [![Image 3: Java Example](#) Java Example](#) The following example demonstrates how to use the `getInetAddress()` method of the `net.Socket` class to connect to a specified host: ## Main.java File ```java import java.net.InetAddress; import java.net.Socket; public class WebPing{ public static void main(String[]args){ try{ InetAddress addr; Socket sock = new Socket("www..com", 80); addr = sock.getInetAddress(); System.out.println("Connected to " + addr); sock.close(); }catch(java.io.IOException e){ System.out.println("Unable to connect to " + args); System.out.println(e); } } } The output of the above code when run is: Connected to http:/www..com/222.73.134.120 [![Image 4: Java Example](#) Java Example](#)
← Net MultisocCollection Rotate β†’