importjava.io.IOException;importjava.net.InetAddress;importjava.net.Socket;importjava.net.SocketException;importjava.net.UnknownHostException;publicclassMain{publicstaticvoidmain(String[] args){try{InetAddress addr =InetAddress.getByName("74.125.67.100");Socket theSocket =newSocket(addr,80);System.out.println("Connected to "+ theSocket.getInetAddress()+" on port "+ theSocket.getPort()+" from port "+ theSocket.getLocalPort()+" of "+ theSocket.getLocalAddress());}catch(UnknownHostException e){System.err.println("I can't find "+ e );}catch(SocketException e){System.err.println("Could not connect to "+e );}catch(IOException e){System.err.println(e);}}}
结果
上面的代码示例将产生以下结果。
Connectedto/74.125.67.100 on port 80 from port
2857 of /192.168.1.4