栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

请求类:获取客户的HTTP请求,分析客户所需要的文件

响应类:获得用户请求后将用户需要的文件读出,添加上HTTP应答头。发送给客户端。

服务器处理类

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)
  {
  }
  }
 }
}

请求类

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 index1)
  return requestString.substring(index1 + 1, index2);
 }
 return null;
 }
}

响应类

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 OKrn" +
  "Content-Type: text/htmlrn" +
  "Content-Length: "+ch+"rn" +
  "rn" +sBody;
  //输出文件
  out.write(sendMessage.getBytes());
 }else {
  // 找不到文件
  String errorMessage = "HTTP/1.1 404 File Not Foundrn" +
  "Content-Type: text/htmlrn" +
  "Content-Length: 23rn" +
  "rn" +
  "File Not Found";
  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服务器的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/139917.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号