教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 资格考试 >

简单Web服务器设计与实现课程设计(4)

来源:网络收集 时间:2026-01-30
导读: System.out.println(您现在可以在浏览器中访问http://localhost:8080/,以测试服务器是否运行); while (true) { Socket socket = null; InputStream input = null; OutputStream output = null; System.out.println

System.out.println("您现在可以在浏览器中访问http://localhost:8080/,以测试服务器是否运行");

while (true)

{

Socket socket = null;

InputStream input = null;

OutputStream output = null;

System.out.println("等待连接...");

socket = serverSocket.accept();

System.out.println(socket.getInetAddress().toString()+"请求连接");

input=socket.getInputStream();

output=socket.getOutputStream();

System.out.println("服务器开始处理第"+(++count)+"次连接");

//开始处理并分析请求信息

Request request=new Request(input);

request.parse();

//开始发送请求资源

Response response=new Response(output);

response.setRequest(request);

response.sendStaticResource();

//关系连接

socket.close();

System.out.println("连接已关闭!");

计算机网络课设报告,java平台实现,Java;HTTP;Web服务器

}

catch(Exception ex)

{

ex.printStackTrace();

System.out.println("3");

//continue;

}

}

private void reBuildConfig(File file)

{

try

{

file.createNewFile();

FileOutputStream fos=new FileOutputStream(file);

PrintStream sp=new PrintStream(fos);

sp.println("WEB_ROOT=");

sp.println("PORT=");

sp.close();

System.out.println("配置文件Config.ini创建成功,您可以修改WEB_ROOT的值改变网页文件的存放路径,修改PORT的值改变访问端口!");

}

catch (IOException ex)

{

ex.printStackTrace();

System.out.println("重建配置文件Config.ini失败");

System.out.println("服务器将使用默认配置...");

}

}

private void reBuildWeb(File file)

{

try

{

file.createNewFile();

FileOutputStream fos=new FileOutputStream(file);

PrintStream sp=new PrintStream(fos);

sp.println("<html>");

sp.println("<head>");

sp.println("<title>简单Web服务器</title>");

sp.println("</head>");

sp.println("<body>");

sp.println("<div align="+"center"+">服务器已经成功运行 </div>");

sp.println("</body>");

sp.println("</html>");

sp.close();

计算机网络课设报告,java平台实现,Java;HTTP;Web服务器

catch (Exception ex)

{

ex.printStackTrace();

}

}

}

-----------------------Request.java------------------------------------ package webserver;

import java.io.InputStream;

public class Request

{

public Request()

{

}

private InputStream input;

private String uri;

public Request(InputStream input)

{

this.input = input;

}

public void parse()//取得请求信息

{

StringBuffer request = new StringBuffer(2048);

int i;

byte[] buffer = new byte[2048];

try

{

i = input.read(buffer);

}

catch (Exception ex)

{

ex.printStackTrace();

i = -1;

}

for (int j = 0; j < i; j++)

{

request.append((char) buffer[j]);

}

System.out.println(request.toString());

uri = parseUri(request.toString());

System.out.println("用户请求:"+this.getUri());

}

private String parseUri(String requestString)//分析请求信息,并返回 {

计算机网络课设报告,java平台实现,Java;HTTP;Web服务器

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;

}

public String getUri()

{

if (pareTo("/")==0)

{

uri="/index.htm";

}

if (uri.indexOf(".")==-1)

{

uri+=".htm";

}

return uri;

}

}

----------------------------Response.java------------------------------ package webserver;

import java.io.OutputStream;

import java.io.IOException;

import java.io.FileInputStream;

import java.io.File;

public class Response

{

private static final int BUFFER_SIZE = 1024;

Request request;

OutputStream output;

public Response(OutputStream output)

{

this.output = output;

}

public void setRequest(Request request)

{

this.request = request;

}

public void sendStaticResource()//发送请求资源

…… 此处隐藏:1246字,全部文档内容请下载后查看。喜欢就下载吧 ……
简单Web服务器设计与实现课程设计(4).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/96978.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)