⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.java

📁 包含多个java程序
💻 JAVA
字号:

//Test.java
import java.io.*;
import java.net.*;
import java.util.*;

public class Test 
{
	public static void main(String args[]) throws IOException {
		if(args.length>0) {
			if(args[0].equals("s"))
				new Server();
			else
				new Client( args.length>1 ? args[1] : "localhost" );
		}
	}
}

class Server
{
	public Server() throws IOException {
		ServerSocket server = new ServerSocket(2000);
		try {
			System.out.println("Listening...");
			while(true) {
				new Handler(server.accept()).start();
				System.out.println("Waiting for next client...");
			}
		} finally {
			server.close();
		}
	}

	class Handler extends Thread
	{
		private Socket sock;
		
		public Handler(Socket s){sock = s;}
		public void run() {
			try {
				ObjectOutputStream output = new ObjectOutputStream(sock.getOutputStream());
				output.writeObject(new Date());
				output.flush();
				sock.close();
			} catch (IOException e) {}
		}
	}
}

class Client
{
	public Client(String host) throws IOException {
		Socket sock = new Socket(host,2000);
		try {
			ObjectInputStream input = new ObjectInputStream(sock.getInputStream());
			Object obj = input.readObject();
			if(obj instanceof Date) {
				Date d = (Date)obj;
				System.out.println(d.getHours()+":"+d.getMinutes()+":"+d.getSeconds());
			} 
		} catch (Exception e) {}
		finally {
			sock.close();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -