Connect with a Web server with Socket in Java
Description
The following code shows how to connect with a Web server with Socket.
Example
import java.net.InetAddress;
import java.net.Socket;
//ww w .j a va 2 s . c om
public class Main {
public static void main(String[] args) throws Exception {
InetAddress addr;
Socket sock = new Socket("www.java2s.com", 80);
addr = sock.getInetAddress();
System.out.println("Connected to " + addr);
sock.close();
}
}
The code above generates the following result.