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

📄 client.java

📁 实现网络客户端、单/多线程网络服务器的功能Exercise 1 of SSD 8
💻 JAVA
字号:
import java.net.*;
import java.io.*;

/**
 * Class <em>Client</em> is a class representing a simple
 * HTTP client.
 *
 * @author iCarnegie, Inc (CTE)
 * @version 1.0
 */

public class Client
{

    /**
     * default HTTP port is port 8000
     */
    private static int port = 8000;

    /**
     * Allow a maximum buffer size of 192 bytes
     */
    private static int buffer_size = 8192;

    /**
     * The end of line character sequence.
     */
    private static String CRLF = "\r\n";


    /**
     * Input is taken from the keyboard
     */
    static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

    /**
     * Output is written to the screen (standard out)
     */
    static PrintWriter screen = new PrintWriter (System.out, true);

    public static void main( String [] args ) {

	try {
	    /**
	     * Create a new HttpClient object.
	     */
	    HttpClient myClient = new HttpClient();

	    /**
	     * Parse the input arguments.
	     */
	    if (args.length != 1) {
		System.err.println("Usage: Client <server>");
		System.exit(0);
	    }

	    /**
	     * Connect to the input server
	     */
	    myClient.connect( args[0] );

	    /**
	     * Read the get request from the terminal.
	     */
	    screen.println(args[0] + " is listening to your request:");
	    String request = keyboard.readLine();

	    /**
	     * Be sure to add the necessary end-of-line characters to
	     * the request.
	     */
	    request += CRLF + CRLF;

	    /**
	     * Ask the client to process the GET request.
	     */
	    myClient.processGetRequest( request );

	    /**
	     * Get the headers and display them.
	     */
	    screen.println( "Header: \n" );
	    screen.print( myClient.getHeader() + "\n" );
	    screen.flush();

	    /**
	     * Ask the user to input a name to save the resultant web page.
	     */
	    screen.println();
	    screen.print("Enter the name of the file to save: ");
	    screen.flush();
	    String filename = keyboard.readLine();
	    FileOutputStream outfile = new FileOutputStream(filename);

	    /**
	     * Save the response to the specified file.
	     */
	    String response = myClient.getResponse();
	    outfile.write( response.getBytes() );
	    outfile.flush();
	    outfile.close();

	    /**
	     * Close the connection client.
	     */
	    myClient.close();
	} catch( Exception e ) {
	    e.printStackTrace();
	}
    }
}

⌨️ 快捷键说明

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