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

📄 client.java

📁 S S D 1 练习的代码
💻 JAVA
字号:
/*
 * Created on 2008-3-20
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * SSD8 EX 1
 * @author Fantao
 * Client.java
 */

import java.io.*;  
import java.net.*;  
  
  
class Client {  
  
  /*  
   * A client connect with a server and send some commands. 
   * Save the response from the server.  
   *   
   * argv[] is the address of server.   
   */  
	
  public static void main(String argv[]) throws Exception {  
  
    /* 
     * Reader and writer 
     */
  	
    BufferedReader stdIn = new BufferedReader(
    	new InputStreamReader(System.in));  
    PrintWriter stdOut = new PrintWriter(  
        new OutputStreamWriter(System.out), true);  
  
    /*  
     * If the lenth of argv isn't 1, write the error message and return.      
     */ 
    
    if (argv.length != 1) {  
      stdOut.println("Usage: Client <server>");  
      return;  
    }  
  
    /* client socket */ 
    Socket clientSocket = new Socket(argv[0], 80);  
  
    /* Reader and writer between client and server */  
    
    BufferedReader inFromServer = new BufferedReader(new InputStreamReader(  
        clientSocket.getInputStream()));  
    PrintWriter outToServer = new PrintWriter(new OutputStreamWriter(  
        clientSocket.getOutputStream()), true);  
  
    stdOut.println(argv[0] + " is listening to your request:");  
  
    /* the commands read from user */ 
    
    String command = "";  
  
    /* the response temporarily read one by one */ 
    
    String tempread = "";  
  
    /* the web information and html sentencesread from server */ 
    
    String info1 = "";  
    String info2 = "";  
  
    /* the condition whether reading sentences html or web information */ 
    
    boolean htmlorweb = false;  
  
    command = stdIn.readLine();  
  
    outToServer.println(command + "\r\n\r\n");  
  
    /*  
     * Read the response from server and identify the content.    
     */  
    
    while (true) {  
      tempread = inFromServer.readLine();  
      if (tempread == null)  
        break;  
      if (htmlorweb == false && tempread.length() == 0)  
        htmlorweb = true;  
      if (htmlorweb == false)  
        info2 = info2 + "\n" + tempread;  
      else  
        info1 = info1 + "\n" + tempread;  
    }  
  
    stdOut.println("Header:\n" + info2);  
  
    /*  
     * Write the html sentences into a html file.    
     */  
    
    stdOut.print("Enter the name of the file to save:");  
    stdOut.flush();  
  
    command = stdIn.readLine();  
  
    PrintWriter fileOut = new PrintWriter(new FileWriter(command), true);  
    fileOut.println(info1);  
  
    /*  
     * Close all the reader and writer.    
     */ 
    fileOut.close();  
    inFromServer.close();  
    outToServer.close();  
    stdIn.close();  
    stdOut.close();  
  
  }  
}

⌨️ 快捷键说明

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