java使用socket實(shí)現(xiàn)一個(gè)多線程web服務(wù)器的方法
除了服務(wù)器類,還包括請(qǐng)求類和響應(yīng)類
請(qǐng)求類:獲取客戶的HTTP請(qǐng)求,分析客戶所需要的文件
響應(yīng)類:獲得用戶請(qǐng)求后將用戶需要的文件讀出,添加上HTTP應(yīng)答頭。發(fā)送給客戶端。
服務(wù)器處理類
package com.lp.app.webserver; import java.io.*; import java.net.*; //使用Socket創(chuàng)建一個(gè)WEB服務(wù)器,本程序是多線程系統(tǒng)以提高反應(yīng)速度。 class WebServer { public static String WEBROOT = "";//默認(rèn)目錄 public static String defaultPage = "index.htm";//默認(rèn)文件 public static void main (String [] args) throws IOException { System.out.println ("服務(wù)器啟動(dòng)...\n"); //使用8080端口提供服務(wù) ServerSocket server = new ServerSocket (8080); while (true) { //阻塞,直到有客戶連接 Socket sk = server.accept (); System.out.println ("Accepting Connection...\n"); //啟動(dòng)服務(wù)線程 new WebThread (sk).start (); } } } //使用線程,為多個(gè)客戶端服務(wù) class WebThread extends Thread { private Socket sk; WebThread (Socket sk) { this.sk = sk; } //線程體 public void run () { InputStream in = null; OutputStream out = null; try{ in = sk.getInputStream(); out = sk.getOutputStream(); //接收來(lái)自客戶端的請(qǐng)求。 Request rq = new Request(in); //解析客戶請(qǐng)求 String sURL = rq.parse(); System.out.println("sURL="+sURL); if(sURL.equals("/")) sURL = WebServer.defaultPage; Response rp = new Response(out); rp.Send(sURL); } catch (IOException e) { System.out.println (e.toString ()); } finally { System.out.println ("關(guān)閉連接...\n"); //最后釋放資源 try{ if (in != null) in.close (); if (out != null) out.close (); if (sk != null) sk.close (); } catch (IOException e) { } } } }
請(qǐng)求類
package com.lp.app.webserver; import java.io.*; import java.net.*; //獲取客戶的HTTP請(qǐng)求,分析客戶所需要的文件 public class Request{ InputStream in = null; //獲得輸入流。這是客戶的請(qǐng)求數(shù)據(jù)。 public Request(InputStream input){ this.in = input; } //解析客戶的請(qǐng)求 public String parse() { //從Socket讀取一組數(shù)據(jù) StringBuffer requestStr = new StringBuffer(2048); int i; byte[] buffer = new byte[2048]; try { i = in.read(buffer); } catch (IOException e) { e.printStackTrace(); i = -1; } for (int j=0; j<i; j++) { requestStr.append((char) buffer[j]); } System.out.print(requestStr.toString()); return getUri(requestStr.toString()); } //獲取URI信息字符 private String getUri(String requestString) { int index1, index2; index1 = requestString.indexOf(' '); if (index1 != -1) { index2 = requestString.indexOf(' ', index1 + 1); if (index2 > index1) return requestString.substring(index1 + 1, index2); } return null; } }
響應(yīng)類
package com.lp.app.webserver; import java.io.*; import java.net.*; //獲得用戶請(qǐng)求后將用戶需要的文件讀出,添加上HTTP應(yīng)答頭。發(fā)送給客戶端。 public class Response{ OutputStream out = null; //發(fā)送請(qǐng)求的文件 public void Send(String ref) throws IOException { byte[] bytes = new byte[2048]; FileInputStream fis = null; try { //構(gòu)造文件 File file = new File(WebServer.WEBROOT, ref); if (file.exists()) { //構(gòu)造輸入文件流 fis = new FileInputStream(file); int ch = fis.read(bytes, 0, 2048); //讀取文件 String sBody = new String(bytes,0); //構(gòu)造輸出信息 String sendMessage = "HTTP/1.1 200 OK\r\n" + "Content-Type: text/html\r\n" + "Content-Length: "+ch+"\r\n" + "\r\n" +sBody; //輸出文件 out.write(sendMessage.getBytes()); }else { // 找不到文件 String errorMessage = "HTTP/1.1 404 File Not Found\r\n" + "Content-Type: text/html\r\n" + "Content-Length: 23\r\n" + "\r\n" + "<h1>File Not Found</h1>"; out.write(errorMessage.getBytes()); } } catch (Exception e) { // 如不能實(shí)例化File對(duì)象,拋出異常。 System.out.println(e.toString() ); } finally { if (fis != null) fis.close(); } } //獲取輸出流 public Response(OutputStream output) { this.out = output; } }
以上這篇java使用socket實(shí)現(xiàn)一個(gè)多線程web服務(wù)器的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringMVC 重新定向redirect請(qǐng)求中攜帶數(shù)據(jù)方式
這篇文章主要介紹了SpringMVC 重新定向redirect請(qǐng)求中攜帶數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12java父子節(jié)點(diǎn)parentid樹形結(jié)構(gòu)數(shù)據(jù)的規(guī)整
這篇文章主要介紹了java父子節(jié)點(diǎn)parentid樹形結(jié)構(gòu)數(shù)據(jù)的規(guī)整,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07Spring?boot事務(wù)無(wú)效報(bào)錯(cuò):Transaction?not?enabled問題排查解決
在業(yè)務(wù)代碼中經(jīng)常需要保證事務(wù)的原子性,但是有的時(shí)候確實(shí)是出現(xiàn)事務(wù)沒有生效,這篇文章主要給大家介紹了關(guān)于Spring?boot事務(wù)無(wú)效報(bào)錯(cuò):Transaction?not?enabled問題排查的相關(guān)資料,需要的朋友可以參考下2023-11-11springboot單獨(dú)使用feign簡(jiǎn)化接口調(diào)用方式
這篇文章主要介紹了springboot單獨(dú)使用feign簡(jiǎn)化接口調(diào)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03