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

📄 networkclienttest.java

📁 nanjing university cs 的java课件。 对新手很有用。付课件中源码。
💻 JAVA
字号:
import java.net.*;
import java.io.*;

public class NetworkClientTest{
	public static void main(String[] args) {
		String host = "mes.nju.edu.cn";
		if(args.length > 0)
			host = args[0];
		int port = 110;
		if(args.length > 1)
			port = Integer.parseInt(args[1]);
		NetworkClient nwClient = new NetworkClient(host, port);
		nwClient.connect();
	}
}

class NetworkClient{
	private String host;
	private int port;
	
	public NetworkClient(String host, int port){
		this.host = host;
		this.port = port;	
	}
	
	public String getHost(){
		return this.host;
	}
	
	public int getPort(){
		return this.port;
	}
	
	public void connect(){
		try{
			Socket client = new Socket(host, port);
			handleConnection(client);
		}
		catch(UnknownHostException uhe){
			System.out.println("Unknown host: " + host);
			uhe.printStackTrace();
		}
		catch(IOException ioe){
			System.out.println("IOException: " + ioe);
			ioe.printStackTrace();
		}
	}
	
	void handleConnection(Socket client) throws IOException{
		PrintWriter out = SocketUtil.getPrintWriter(client);
		BufferedReader in = SocketUtil.getBufferedReader(client);
		System.out.println("<< " + in.readLine());
		out.println("USER uliyas");
		System.out.println(">> USER uliyas");
		System.out.println("<< " + in.readLine());
		out.println("PASS 1234");
		System.out.println(">> PASS 1234");
		System.out.println("<< " + in.readLine());
		client.close();
	}
}

class SocketUtil{
	public static BufferedReader getBufferedReader(Socket s) throws IOException{
		return new BufferedReader(new InputStreamReader(s.getInputStream()));
	}
	
	public static PrintWriter getPrintWriter(Socket s) throws IOException{
		return new PrintWriter(s.getOutputStream(), true);
	}
}

⌨️ 快捷键说明

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