好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java使用socket实现一个多线程web服务器的方法

除了服务器类,还包括请求类和响应类

请求类:获取客户的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.*;

 

//使用 socket 创建一个 web服务器 ,本程序是多线程系统以提高反应速度。

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

查看更多关于java使用socket实现一个多线程web服务器的方法的详细内容...

  阅读:11次