Net Connected
# Java Example - Connect to a Specified Host Using Socket
[ 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
[ Java Example](#)
YouTip