Java 实例 - Socket 实现多线程服务器程序
以下实例演示了如何使用 Socket 类的 accept() 方法和 ServerSocket 类的 MultiThreadServer(socketname) 方法来实现多线程服务器程序:
import java.io.IOException; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; public class MultiThreadServer implements Runnable { Socket csocket; MultiThreadServer(Socket csocket) { this.csocket = csocket; } public static void main(String args[]) throws Exception { ServerSocket ssock = new ServerSocket(1234); System.out.println("Listening"); while (true) { Socket sock = ssock.accept(); System.out.println("Connected"); new Thread(new MultiThreadServer(sock)).start(); } } public void run() { try { PrintStream pstream = new PrintStream (csocket.getOutputStream()); for (int i = 100; i >= 0; i--) { pstream.println(i + " bottles of beer on the wall"); } pstream.close(); csocket.close(); } catch (IOException e) { System.out.println(e); } } }
以上代码运行输出结果为:
Listening Connected
Java 实例 - 查看主机指定文件的最后修改时间
以下实例演示了如何查看主机指定文件的最后修改时间:
import java.net.URL; import java.net.URLConnection; import java.util.Date; import java.text.SimpleDateFormat; public class Main { public static void main(String[] argv) throws Exception { URL u = new URL("http://127.0.0.1/test/test.html"); URLConnection uc = u.openConnection(); SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss"); uc.setUseCaches(false); long timestamp = uc.getLastModified(); System.out.println( "test.html 文件最后修改时间 :" + ft.format(new Date(timestamp))); } }
以上代码运行输出结果为:
test.html 文件最后修改时间 :2018-09-06 10:06:04
Java 实例 - 使用 Socket 连接到指定主机
以下实例演示了如何使用 net.Socket 类的getInetAddress() 方法来连接到指定主机
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.nowcoder.com", 80); addr = sock.getInetAddress(); System.out.println("连接到 " + addr); sock.close(); } catch (java.io.IOException e) { System.out.println("无法连接 " + args[0]); System.out.println(e); } } }
以上代码运行输出结果为:
连接到 http:/www.nowcoder.com/222.73.134.120
Java 实例 - 网页抓取
以下实例演示了如何使用 net.URL 类的 URL() 构造函数来抓取网页:
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.InputStreamReader; import java.net.URL; public class Main { public static void main(String[] args) throws Exception { URL url = new URL("http://www.nowcoder.com"); BufferedReader reader = new BufferedReader (new InputStreamReader(url.openStream())); BufferedWriter writer = new BufferedWriter (new FileWriter("data.html")); String line; while ((line = reader.readLine()) != null) { System.out.println(line); writer.write(line); writer.newLine(); } reader.close(); writer.close(); } }
以上代码运行输出结果为(网页的源代码,存储在当前目录下的 data.html 文件中):
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <meta http-equiv="X-UA-Compatible" content="IE=11,IE=10,IE=9,IE=8"/>……
Java 实例 - 获取 URL响应头的日期信息
以下实例演示了如何使用 HttpURLConnection 的 httpCon.getDate() 方法来获取 URL响应头的日期信息:
import java.net.HttpURLConnection; import java.net.URL; import java.util.Date; public class Main{ public static void main(String args[]) throws Exception { URL url = new URL("http://www.nowcoder.com"); HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); long date = httpCon.getDate(); if (date == 0) System.out.println("无法获取信息。"); else System.out.println("Date: " + new Date(date)); } }
以上代码运行输出结果为:
Date: Mon May 04 11:57:06 CST 2015