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

📄 modifiedclient.java

📁 ssd8练习一答案 如有差错 多多包涵 大家参考时注意一下
💻 JAVA
字号:
import java.io.*;
import java.net.*;

public class ModifiedClient {
	//default constructor
	public ModifiedClient(){}
	static int numOfByteSend = 0;
	static int numOfByteReceive = 0;
	static String request = "";
	/*
	 * this function is used to send the client's reguest to the server
	 * 
	 * */
	public void sendMessage(BufferedReader inFromClient,Socket clientSocket,String mesg){
		
		System.out .println(mesg + " is listening to your request : \n");
		
		try{
			DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
			request = inFromClient.readLine();
			outToServer.writeBytes(request + "\r\n\r\n");
			//count the number of bytes that it sends
			numOfByteSend = request.getBytes().length;
		}catch(IOException ioe){
			System.err.println("IO ERROR : "); 
			ioe.printStackTrace();
		}
		
	}
	/*
	 * this method is used to get the header from the server reply
	 * */
	public void header(BufferedReader inFromServer){
	
		String header = "";
		String str  = "";
		try{
			str = inFromServer.readLine();
			System.out .println(str);
			while(str.length() != 0){ 
				header += str;
				str = inFromServer.readLine();
				System.out .println(str);
			}
			//count the number of bytes that it receive
			numOfByteReceive = header.getBytes().length;
		}catch(IOException ioe){
			System.err .println("IO ERROR : ");
			ioe.printStackTrace() ;
		}
		//System.out.println("\r\n" + "HEADER : " + header + "\n"); 
	}
	/*
	 *this function is used to change the flow that come from server to the type of "byte".
	 * */
	public byte[] seReply(BufferedReader inFromServer){

		String string = "";
		String receive = "";
		//get every byte from receiveFile to buffer
		try{
			
			string = inFromServer.readLine();
			while(string != null){
				receive += string;
				string = inFromServer.readLine();
			}
			//count the other part's number of bytes that it receive
			numOfByteReceive += receive.getBytes().length;
		}catch(IOException ioe){
			System.err .println("IO ERROR : ");
			ioe.printStackTrace() ;
		}
		return receive.getBytes();
	}
	
	/*
	 * this function is used to save the information to the local file
	 * */
	public void saveFile(BufferedReader inFromClient,byte[] buffer){
		
		
		try{
			System.out .println("Please input the file name to save the data : ");
			FileOutputStream file = new FileOutputStream(inFromClient.readLine());
			PrintStream p = new PrintStream(file);
			p.write(buffer);
			p.close();
		}catch(FileNotFoundException fnfe){
			System.err .println("The file not exist");
			fnfe.printStackTrace() ;
		}catch(IOException ioe){
			System.err .println("IO ERROR : ");
			ioe.printStackTrace();
		}
		
	}

	public static void main(String argv[])throws Exception{

		//creat a new client object and a socket
		ModifiedClient client = new ModifiedClient();
		Socket clientSocket = null;
		
		//Open a TCP connection to port 80 of server
		try{
			clientSocket = new Socket(argv[0],80);
			
		}catch(UnknownHostException uke){
			
			System.err.println("The host is unknown! ");
			uke.printStackTrace();
		}catch(IOException ioe){
			
			System.err .println("IO ERROR : ");
			ioe.printStackTrace();
		}catch(SecurityException se){
			
			System.err .println("we can not get throw it for security exception");
			se.printStackTrace();
		}
		BufferedReader inFromClient = new BufferedReader(new InputStreamReader(System.in));
	
		BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
				clientSocket.getInputStream()));
		
		
		client.sendMessage(inFromClient,clientSocket,argv[0]);
		client.header(inFromServer);
		byte[] buffer = client.seReply(inFromServer) ;
		System.out.println("the number of Bytes that it send: " + numOfByteSend);
		System.out.println("the number of Bytes that it send: " + numOfByteReceive + "\n");
		client.saveFile(inFromClient,buffer) ;
		//close the socket
		clientSocket.close() ;
	}
}

⌨️ 快捷键说明

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