在Java编程中,如何与Web服务器连接?
以下示例演示如何使用net.Socket
类的sock.getInetAddress()
方法与Web服务器连接。
package com.yiibai;
import java.net.InetAddress;
import java.net.Socket;
public class Connected2Server {
public static void main(String[] args) {
try {
InetAddress addr;
Socket sock = new Socket("www.yiibai.com", 80);
addr = sock.getInetAddress();
System.out.println("Connected to " + addr);
sock.close();
} catch (java.io.IOException e) {
System.out.println("Can‘t connect to " + args[0]);
System.out.println(e);
}
}
}
上述代码示例将产生以下结果 -
Connected to www.yiibai.com/112.124.103.85
原文:https://www.cnblogs.com/borter/p/9617164.html