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();
//接收來自客戶端的請(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)文章
Java Spring Cloud Bus 實(shí)現(xiàn)配置實(shí)時(shí)更新詳解
這篇文章主要介紹了SpringCloud Bus如何實(shí)現(xiàn)配置刷新,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-09-09
Springboot指定掃描路徑的實(shí)現(xiàn)示例
本文主要介紹了Springboot指定掃描路徑的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05
SpringBoot使用JUL實(shí)現(xiàn)日志記錄功能
在SpringBoot中,我們可以使用多種日志框架進(jìn)行日志記錄,其中,JUL(Java Util Logging)是Java平臺(tái)自帶的日志框架,它提供了簡(jiǎn)單的 API 和配置,可以輕松地進(jìn)行日志記錄,本文將介紹如何在 SpringBoot中使用JUL進(jìn)行日志記錄,并提供示例代碼2023-06-06
@PathVariable為空時(shí)指定默認(rèn)值的操作
這篇文章主要介紹了@PathVariable為空時(shí)指定默認(rèn)值的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02
SpringBoot預(yù)防XSS攻擊的實(shí)現(xiàn)
XSS攻擊是一種在web應(yīng)用中的計(jì)算機(jī)安全漏洞,它允許惡意web用戶將代碼植入到提供給其它用戶使用的頁面,本文主要介紹了SpringBoot預(yù)防XSS攻擊的實(shí)現(xiàn),感興趣的可以了解一下2023-08-08
idea打開運(yùn)行配置java?web項(xiàng)目的全過程
這篇文章主要給大家介紹了關(guān)于idea打開運(yùn)行配置java?web項(xiàng)目的相關(guān)資料,有些時(shí)候我們用IDEA跑之前用eclipse中運(yùn)行的項(xiàng)目的時(shí)候,總是不止所措,要不就是只展示html,要不就是不能部署成功,需要的朋友可以參考下2023-08-08
SpringBoot項(xiàng)目中引入本地JAR包配置的幾種方法
SpringBoot有時(shí)需要引入本地JAR包以便重用已有的代碼庫或者第三方庫,本文主要介紹了SpringBoot項(xiàng)目中引入本地JAR包配置的幾種方法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-08-08
在springboot文件中如何創(chuàng)建mapper.xml文件
這篇文章主要介紹了在springboot文件中如何創(chuàng)建mapper.xml文件問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01

