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

📄 client.java

📁 SSD8练习1答案
💻 JAVA
字号:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/**
 * Creates a client program, and the users can use it to connect the server and send some requests to the server  
 * @author liukui
 *
 */
public class Client {

	/*standard input stream*/
	private static BufferedReader stdIn =
		new BufferedReader(new InputStreamReader(System.in));
	
	/*standard output stream*/
	private static PrintWriter stdOut =
		new PrintWriter(System.out);
	
	/*a connection port 80 to the server*/
	private static final int PORT = 80;
	
	/*a socket to the server*/
	Socket client;
	
	/*a command got from the user */
	private String command = "";
	
	/*the server's name*/
	private String server = "";
	
	/*the constructor of the Client, and the parameter is the server's name*/
	public Client(String server) {
		
		this.server = server;
	}
	
	/*
	 * a method connects to the server*/
	public void connect() {
		try {
			client = new Socket(this.server,PORT);
		} catch(Exception e) {
			stdOut.println("The connection failed!");
			stdOut.flush();
			System.exit(0);
		}
		stdOut.println(this.server + "is listening to your request");
		stdOut.flush();
	}
	
	/*
	 * the user send a request to the sever*/
	public void request() {
		try {
			command = stdIn.readLine() + "\r\n\r\n";
			DataOutputStream outToServer = 
				new DataOutputStream(client.getOutputStream());
			byte[] buffer = command.getBytes();
			outToServer.write(buffer,0,command.length());
			//outToServer.writeBytes(command +'\n');
		
		} catch(Exception e) {
			stdOut.println("Bad request!");
			stdOut.flush();
		}
	}
	/*
	 * response from the server */
	public void getInformation() {
		try {
			BufferedReader inFromServer = 
				new BufferedReader(new InputStreamReader(client.getInputStream()));
			/*output the header to the screen*/
			stdOut.println("Header:"+"\r\n\r\n");
			String read = inFromServer.readLine();
			while(read.length()!= 0) {
				stdOut.println(read);
				stdOut.flush();
				read = inFromServer.readLine();
			}
			stdOut.println("\nplease input the name which you want to save as:");
			stdOut.flush();
			String filename = stdIn.readLine();
			
			/*output the content to the file which you want to save as*/
			PrintWriter fileOut =
			    new PrintWriter(new FileWriter(filename));
			
			//read = inFromServer.readLine();
			while(inFromServer.ready()) {
				read = inFromServer.readLine();
				fileOut.println(read);
				fileOut.flush();
			}
			fileOut.println(read);
			fileOut.flush();
			inFromServer.close();
			fileOut.close();
		} catch(Exception e) {
			stdOut.println("can't get any information from the server");
			stdOut.flush();
		}
		
	}
	public static void main(String args[]) throws Exception {
		
		if(args.length != 1) {
			stdOut.println("Usage Client <server>");
			stdOut.flush();
		}
		else {
			Client c = new Client(args[0]);
			c.connect();
			c.request();
			c.getInformation();
			c.client.close();
		}
	}
}

⌨️ 快捷键说明

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