除了服務器類,還包括請求類和響應類
請求類:獲取客戶的http請求,分析客戶所需要的文件
響應類:獲得用戶請求后將用戶需要的文件讀出,添加上http應答頭。發送給客戶端。
服務器處理類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
package com.lp.app.webserver; import java.io.*; import java.net.*; class webserver { public static string webroot = "" ; //默認目錄 public static string defaultpage = "index.htm" ; //默認文件 public static void main (string [] args) throws ioexception { system.out.println ( "服務器啟動...\n" ); //使用8080端口提供服務 serversocket server = new serversocket ( 8080 ); while ( true ) { //阻塞,直到有客戶連接 socket sk = server.accept (); system.out.println ( "accepting connection...\n" ); //啟動服務線程 new webthread (sk).start (); } } } //使用線程,為多個客戶端服務 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(); //接收來自客戶端的請求。 request rq = new request(in); //解析客戶請求 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 ( "關閉連接...\n" ); //最后釋放資源 try { if (in != null ) in.close (); if (out != null ) out.close (); if (sk != null ) sk.close (); } catch (ioexception e) { } } } } |
請求類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
package com.lp.app.webserver; import java.io.*; import java.net.*; //獲取客戶的http請求,分析客戶所需要的文件 public class request{ inputstream in = null ; //獲得輸入流。這是客戶的請求數據。 public request(inputstream input){ this .in = input; } //解析客戶的請求 public string parse() { //從socket讀取一組數據 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 ; } } |
響應類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
package com.lp.app.webserver; import java.io.*; import java.net.*; //獲得用戶請求后將用戶需要的文件讀出,添加上http應答頭。發送給客戶端。 public class response{ outputstream out = null ; //發送請求的文件 public void send(string ref) throws ioexception { byte [] bytes = new byte [ 2048 ]; fileinputstream fis = null ; try { //構造文件 file file = new file(webserver.webroot, ref); if (file.exists()) { //構造輸入文件流 fis = new fileinputstream(file); int ch = fis.read(bytes, 0 , 2048 ); //讀取文件 string sbody = new string(bytes, 0 ); //構造輸出信息 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) { // 如不能實例化file對象,拋出異常。 system.out.println(e.tostring() ); } finally { if (fis != null ) fis.close(); } } //獲取輸出流 public response(outputstream output) { this .out = output; } } |
以上這篇java使用socket實現一個多線程web服務器的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/luanpeng825485697/article/details/78229419