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

📄 proxyserver.java

📁 ssd8考试一 计算机网络 A Web proxy server listens to requests, then retrieves the requested files from othe
💻 JAVA
字号:
/*
 * Created on 2008-4-10
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

import java.io.*;  
import java.net.*;  
import java.util.StringTokenizer;  
  
/**  
 * A Web proxy server which is both a client and a server at the same time.  
 *   
 * @author Taobinbin 053732  
 */  
  

class Handler extends Thread {  //using thread to do the client
	
	private Socket clientSocket ; 
	private Socket serverSocket ; 
	
	
	public Handler(Socket client) throws IOException {
		
		serverSocket = client ; 
		
	}
	
	public void run(){
		
		String request ; //the message in the first line that needed 
		String plusMessage ; //the plusMessage that only to pass
		String target ; 
		String path ;
		int port ; 
		
		try{
			//get the message from the client
		DataInputStream inFromClient = new DataInputStream(serverSocket.getInputStream()); 
		DataOutputStream outToClient = new DataOutputStream(serverSocket.getOutputStream());
		
		request = inFromClient.readUTF();//get the message
		plusMessage = inFromClient.readUTF()+ "\r\n\r\n" ;
		plusMessage += inFromClient.readUTF();// get the plusMessage
		
		   //manage the message to get information useful
		StringTokenizer tokenizedLine = new StringTokenizer(request, "/");
		path = tokenizedLine.nextToken(); //pass the null string
		path = tokenizedLine.nextToken(); //get the path between the second '/' and the third '/'
		StringTokenizer tokenPath = new StringTokenizer(path , ":") ; 
		
		target =  tokenPath.nextToken() ; 
		if(tokenPath.hasMoreTokens())
			port = Integer.parseInt(tokenPath.nextToken()) ; //if there is a port given get it
		else 
			port = 80 ;//if the port is not been given,it will be 80;
		
		//connect to the server
		System.out.println(target) ; 
		System.out.println(port) ; 
		serverSocket = new Socket(target , port) ;
		DataInputStream inFromServer = null;
		DataOutputStream outToServer = null;
		
		if(!serverSocket.isConnected()){
			
			System.out.println("connect server failed!") ; 
			System.exit(1) ; 
			
		}else{
			//get and send message to the server
			inFromServer = new DataInputStream(serverSocket.getInputStream());
			outToServer = new DataOutputStream(serverSocket.getOutputStream());
		
			//write to server
			outToServer.writeBytes(request + "\r\n\r\n"
				+ plusMessage + "\r\n\r\n") ;
			String content ; 
			while((content = inFromServer.readUTF()).length() != 0){

				outToClient.writeUTF(content); 
			}
			while((content = inFromServer.readUTF()) != null ){

				outToClient.writeUTF(content); 
			}
		}
		 
		clientSocket.close() ; //close the clientSocket
		serverSocket.close() ; //close the serverSocket
		inFromClient.close();  //close the DataInputStream
		inFromServer.close();  //close the DataInputStream
		outToClient.close();  //close the DataOutputStream
		outToServer.close();  //close the DataOutputStream
		}catch(Exception e){
			System.err.println("Have an exception! Notice!");
			e.printStackTrace();
		}
									
	}
}

public class ProxyServer {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try 
		{
			ServerSocket server = new ServerSocket(8000) ; 
			System.out.println("Server is listenning :\r\n");
			while(true){
				
				Socket client = server.accept();
				
				Handler handler = new Handler(client) ;
				handler.start(); 
			}
		}catch(IOException e){
			
			System.out.println(e.getMessage());
			
			e.printStackTrace();
		}
	}

}	

⌨️ 快捷键说明

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