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

📄 proxyserver.java

📁 开发一个代理服务器
💻 JAVA
字号:
import java.io.*;
import java.net.*;
import java.util.*;


/**
 * @author shenzhiyuan
 * @version 1.0
 * <p>This class is a web proxy server.As a intermediate tools to connect the web you want.
 * Fisrt it will receive the request from the client tools ,and the class then deal with the 
 * request----send the dealed request to the server you want.And soon will receive the server's 
 * infomation ,later return to the client.</p>
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
class ProxyServer
{
	public static void main(String[] args)throws Exception
	{
		//create a server socket with port 8000
		ServerSocket serverSocket = new ServerSocket(8000);
		while (true) {
			
			//wait for a connect 
			Socket connectionSocket = serverSocket.accept();
			
			//create a thread for every connection
			Handler thread = new Handler(connectionSocket);
			thread.start();
		}
	}
}

//This class handle every connection,which extends class thread
class Handler extends Thread
{
	//create a socket connection
	private Socket connectToClient;
	
	public Handler(Socket connetSocket)
	{	
		connectToClient=connetSocket;
	}
	public void run()
	{	
		try {
			
			BufferedReader inFormClient =
				new BufferedReader(new InputStreamReader(connectToClient.getInputStream()));
			DataOutputStream outToClient =
				new DataOutputStream(connectToClient.getOutputStream());
			
			//read the first line from the client
			String requesHeadLine = inFormClient.readLine();
			
			//With class StringTokenizer,the requesting line is divided
			StringTokenizer tokenizedLine = 
				new StringTokenizer(requesHeadLine);
			String nextToken = tokenizedLine.nextToken();
			
			if(nextToken.equals("GET"))
			{
				
				String addressPart = tokenizedLine.nextToken();
				if(addressPart.startsWith("http://"))
				{
					String str=addressPart.substring(7);
					StringTokenizer tokenized =
						new StringTokenizer(str,"/");
					String hostnamePart = tokenized.nextToken();
					
					//get the file name from the request line
					String filename = tokenized.nextToken();
					
					StringTokenizer tokenized2 =
						new StringTokenizer(hostnamePart,":");
					
					//get the hostname from the request line
					String hostname = tokenized2.nextToken();
					String portStr="";
					if(tokenized2.hasMoreTokens())
					{
						portStr = tokenized2.nextToken();
					}
					
					//if there is a port,record the special port
					//if there isn't a port,the port is 80
					int port = 80;
					if(portStr!="")
					{
						port=Integer.valueOf(portStr);
					}
					
					//create a object of Client with the hostname and the special port
					Client clientReq = new Client(hostname,port);
					//Using client's funtion to get the content recieved by the client
					String content = clientReq.getContent(filename);
					
					//save the content in a file
					FileOutputStream outFile = new FileOutputStream(filename);
					outFile.write(content.getBytes());
					
					//display the content to the client
					outToClient.writeBytes(content);
				}
			}else
			{
				outToClient.writeBytes("HTTP/1.1 404 Object Not Found\r\n");
			}
			connectToClient.close();
			
		}catch(IOException ioe)
		{
			System.out.println(ioe);
		}
	}
	
}

//this class is used to handle a client to the special host with the port
class Client
{
	private Socket clientSocket;
	public Client(String hostname,int port) 
	{
		try{
			clientSocket = new Socket(hostname,port);
			
		}catch(IOException ioe)
		{
			System.out.print(ioe);
		}
	}
	//The funtion is used to get the content
	public String getContent(String filename)
	{
		String content="";
		try{
			DataOutputStream outToServer =
				new DataOutputStream(clientSocket.getOutputStream());
			BufferedReader inFromServer =
				new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
		
			String requestLine ="GET " + "/" + filename + " HTTP/1.1";
			outToServer.writeBytes(requestLine + "\n");
			
			String receiveLine = inFromServer.readLine();
			while(receiveLine!= null)
			{
				content+= receiveLine;
				content+= "\r\n";
				receiveLine = inFromServer.readLine();	
			}	
			content+="\r\n";
			String content2 ="";
			receiveLine=inFromServer.readLine();
			while(receiveLine!= null)
			{	
				content2 += receiveLine;
				receiveLine = inFromServer.readLine();
			}
			content+=content2;
			clientSocket.close();
		}catch(IOException ioe)
		{
			System.out.println(ioe);
			
		}
		return content;
	}
}

⌨️ 快捷键说明

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