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

📄 threadedserver.java

📁 多线程编程
💻 JAVA
字号:
package NetWorkpractice;

import java.io.*;
import java.net.*;

public class ThreadedServer {
	public static void main(String[] args){
		int i=1,PORT = 8000;
		ServerSocket server = null;
		Socket client = null;
		try{
			server = new ServerSocket(PORT);
			System.out.println("Web server is listening on port"+ server.getLocalPort());
			for(;;){
				client = server.accept();
				new ConnectionThread(client,i).start();
				i++;
			}
		}catch (Exception e){System.out.println(e);}
	}

}


 class ConnectionThread extends Thread{
	 Socket client;
	 int counter;
	 public ConnectionThread(Socket c1,int c){
		 client = c1;
		 counter =c;
	 }
	 public void run()
	 {
		 try{
			 String destIP = client.getInetAddress().toString();
			 int destport = client.getPort();
			 System.out.println("Connection" +counter+":connected to"+
					 destIP + "on port"+ destport +".");
			 PrintStream outstream = new PrintStream(client.getOutputStream());
			 BufferedReader instream = new BufferedReader(new InputStreamReader(client.getInputStream()));
			 String inline = instream.readLine();
			 System.out.println("Recieved:"+ inline);
			 if(getrequest(inline)){
				 String filename = getfilename(inline);
				 File file =new File(filename);
				 if(file.exists()){
					 System.out.println(filename+" requested.");
					 outstream.println("HTTP/1.0 200 OK");
					 outstream.println("MIME_version:1.0");
					 outstream.println("Content_Type:text/html");
					 int len = (int)file.length();
					 outstream.println("Content_Length:"+ len);
					 outstream.println("");
					 sendfile(outstream,file);
					 outstream.flush();
					 }
				 else{
					 String notfound ="<html><head><title>Not Found</title></head>" +
					  "<body><h1>Error 404-file not found</h1></body></html>";
					 outstream.println("HTTP/1.0 404 no found");
					 outstream.println("Content_Type:text/html");
					 outstream.println("Conten_length:"+notfound.length()+2);
					 outstream.println("");
					 outstream.println(notfound);
					 outstream.flush();
					 
				 }
			 }
			 long m1=1;
			 while(m1<11100000){m1++;}
			 client.close();
		 }
		 catch(IOException e){
			 System.out.println("Exceptiong:"+e);
		 }
	 }
	 
	 boolean getrequest(String s){
		 if(s.length()>0)
		 {
			if(s.substring(0,3).equalsIgnoreCase("GET")) return true;
		 }
		 return false;
	 }
	 
	 String getfilename(String s){
		 String f = s.substring(s.indexOf(' ')+1);
		 f= f.substring(0,f.indexOf(' '));
		 try{
			 if(f.charAt(0)=='/');
			 f=f.substring(1);
		 }
		 catch(StringIndexOutOfBoundsException e){
			 System.out.println("Exception:"+e);
		 }
		 if(f.equals(""))f="index.html";
		 return f;
	 }
	 
	 void sendfile(PrintStream outs,File file){
		 try{
			 DataInputStream in = new DataInputStream(new FileInputStream(file));
			 int len=(int)file.length();
			 byte buf[] = new byte[len];
			 in.readFully(buf);
			 outs.write(buf,0,len);
			 outs.flush();
			 outs.close();
		 }
		 catch(Exception e){
			 System.out.println("Error retrieving file.");
			 System.exit(1);
		 }
	 }
 }

⌨️ 快捷键说明

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