除了服務(wù)器類,還包括請(qǐng)求類和響應(yīng)類

成都創(chuàng)新互聯(lián)主營(yíng)郊區(qū)網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都App制作,郊區(qū)h5小程序設(shè)計(jì)搭建,郊區(qū)網(wǎng)站營(yíng)銷推廣歡迎郊區(qū)等地區(qū)企業(yè)咨詢
請(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" +
"<h2>File Not Found</h2>";
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è)參考,也希望大家多多支持創(chuàng)新互聯(lián)。
當(dāng)前名稱:java使用socket實(shí)現(xiàn)一個(gè)多線程web服務(wù)器的方法
網(wǎng)址分享:http://chinadenli.net/article44/jhgshe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、面包屑導(dǎo)航、軟件開(kāi)發(fā)、微信公眾號(hào)、品牌網(wǎng)站設(shè)計(jì)、App設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)