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

📄 modifiedclient.java

📁 SSD8的练习2 希望能帮助大家 参考学习
💻 JAVA
字号:


import java.net.*;
import java.io.*;

/**  
 * A Simple HTTP/1.0 Client with a Command Line Interface .  
 *   
 * @version 1.0
  **/ 
public class ModifiedClient {
	
	private final String CRLF = "\r\n";
	private final int PORT = 80;
	private String request;
	private BufferedReader stdIn;
	private Socket clientSocket;
	private PrintWriter outToServer;
	private BufferedReader inFromServer;
	
	
	/*constructor:initialize and build connection.*/
	public ModifiedClient (String server) {
		
		try {
			stdIn = new BufferedReader(new InputStreamReader(System.in));			
			clientSocket = new Socket (server, PORT);			
			outToServer = new PrintWriter(
					new OutputStreamWriter(clientSocket.getOutputStream()), true);
			inFromServer = new BufferedReader (
					new InputStreamReader(clientSocket.getInputStream()));
		} catch (UnknownHostException e) {
			System.err.println("You connect to an unknow server!");
		} catch (IOException e) {	
			
			e.printStackTrace();
		}	
	}
	
	/*This method is to prompt user to input request.*/
	public void prompt(String server) {
		
		System.out.println(server + " is listening to your request:");
	}
	
	/* This method read request from user.*/
	public void readRequest() {		
		
		try {
			request = stdIn.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
		
	/* This method send user's request to server.*/
	public void sendRequest() {	
		
		request = request + CRLF + CRLF;
		outToServer.println(request);
	}
	
	/*This method is to get the total of the sent bytes.*/
	private int getSentBytes() {
		
		return request.length();
	}
	
	/*
	 * This method receives the response from server,
	 * print the header part and save the content.
	 */
	public void receiveResponse() {
		
		boolean isContents = false;	//flag whether the read line is content part.
		String temp = "";			//temporary line that read from server.
		String header = "";			//the header
		String contents = ""; 		//the contents
		int receivedBytes = 0;		//the totally number of bytes that receive from server
		
		try {
			while (true) {
				temp = inFromServer.readLine();  
				if (temp == null) {
					break;  
				}
				if (isContents == false && temp.length() == 0) {
					isContents = true;  
				}
				if (isContents == false) {
					if (temp.startsWith("Content-Length:")) {
						String lenStr = temp.substring(16);
						receivedBytes = Integer.parseInt(lenStr);
					}
					temp += CRLF;
					header += temp; 
				}
				else { 
					temp += CRLF;
					contents +=  temp; 
				}
			}  
			
			System.out.println("Header:\n\n" + header);
			header += CRLF;	//add the space line
			receivedBytes += header.length();
  
			/**
			 * save the content to a file.
			 **/  
			System.out.print("\nEnter the name of the file to save: ");  
			System.out.flush(); 			
			String fileName = stdIn.readLine();   
			PrintWriter outFile = new PrintWriter(new FileWriter(fileName), true);  
			outFile.println(contents); 
			
			System.out.println();
			System.out.println("You have sent " + this.getSentBytes() + " bytes to server.");
			System.out.println("You received " + receivedBytes + " bytes from server.");
			
			outFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
	
	/*This method is to close all.*/
	public void close() {
		try {
			if (stdIn != null)
				stdIn.close();
			if (clientSocket != null)
				clientSocket.close();
			if (outToServer != null)
				outToServer.close();
			if (inFromServer != null) 
				inFromServer.close(); 
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public static void main(String[] args) {
		
		if (args.length !=1 ) {
			System.out.println("Usage: Client <server>");  
			return;  
		}
		
		ModifiedClient mc = new ModifiedClient (args[0]);
		mc.prompt(args[0]);
		mc.readRequest();
		mc.sendRequest();
		mc.receiveResponse();
		mc.close();
	}
}

⌨️ 快捷键说明

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