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

📄 threadedserver.java

📁 SSD8 多线程服务器原码 java 版
💻 JAVA
字号:

import java.io.*;
import java.net.*;
import java.util.StringTokenizer;

public class ThreadedServer {
	private int port=8521;
	private ServerSocket welcomeSocket;
	
	public ThreadedServer()throws IOException{
		welcomeSocket=new ServerSocket(port);
	}
	
	public void service(){
		while(true){
			Socket socket=null;
			try{
				System.out.println("Localhost is listen to you:");
				socket=welcomeSocket.accept();
				
				//create a thread for each request socket
				Thread workThread=new Thread(new Handler(socket));
				workThread.start();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	public static void main(String [] argv)throws IOException{
		
		new ThreadedServer().service();
	}
	
	
	class Handler implements Runnable {
		private Socket socket;
		//the constructor
		public Handler(Socket socket){
			this.socket=socket;
		}
		//implement the abstract method run
		public void run(){
		
			try{
				
			BufferedReader inFromClient=new BufferedReader(
					 new InputStreamReader(socket.getInputStream()));
			DataOutputStream outToClient=new DataOutputStream(socket.getOutputStream());
            String request=inFromClient.readLine();
		    StringTokenizer token=new StringTokenizer(request);
		    String filename;
		    //analyse the requst header
		    String flag=token.nextToken();
		    
		    //recognize the get or put method
		    if(flag.equals("GET")||flag.equals("PUT"))
		    {
		    	filename=token.nextToken();
		    	if(filename.startsWith("/"))
		    		filename=filename.substring(1);
		    	File file=new File(filename);
		    	int numOfBytes=(int)file.length();
		    	byte[] fileInBytes =new byte[numOfBytes];
		    	//read the file from the disk
		    	try{
		    	  FileInputStream inFile=new FileInputStream(filename);
		    	
		    	
		    	inFile.read(fileInBytes);
                }catch(Exception e){
                	//return the file not found exception
                	outToClient.writeBytes("Http/1.0 404 error\r\n");
                	outToClient.writeBytes("Connection: close\r\n");
                	outToClient.writeBytes("Content-Type: text/html\r\n");
                	outToClient.writeBytes("\r\n");
                	outToClient.writeBytes("the request content does not exist");
                	Thread.currentThread().stop();
                	//System.out.println("File not find error");
		    	}
		    	//the response header
		    	outToClient.writeBytes("Http/1.0 200 OK\r\n");
		    	if(filename.endsWith(".jpg"))
		    		outToClient.writeBytes("Content-type: image/jpg\r\n");
		    	if(filename.endsWith(".gif"))
		    		outToClient.writeBytes("Content-type: image/gif\r\n");
		    	if(filename.endsWith(".htm")||filename.endsWith(".html"))
		    		outToClient.writeBytes("Content-type: text/html\r\n");
		    	outToClient.writeBytes("\r\n");
		    	//return the request content to the client
		    	outToClient.write(fileInBytes,0,numOfBytes);
		    	
		    }
		    else{
		    	outToClient.writeBytes("Bad Request Message");
		    	
		    }
			
			}catch(Exception e){
				e.printStackTrace();
			}
			//close the socket
			finally{
				try{
					if(socket!=null){socket.close();}
				}catch(Exception e){e.printStackTrace();}
			}
			
		}
		
	}
	
}

	
	

⌨️ 快捷键说明

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