简单Web服务器设计与实现课程设计(4)
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字,全部文档内容请下载后查看。喜欢就下载吧 ……相关推荐:
- [资格考试]石油钻采专业设备项目可行性研究报告编
- [资格考试]2012-2013学年度第二学期麻风病防治知
- [资格考试]道路勘测设计 绪论
- [资格考试]控烟戒烟知识培训资料
- [资格考试]建设工程安全生产管理(三类人员安全员
- [资格考试]photoshop制作茶叶包装盒步骤平面效果
- [资格考试]授课进度计划表封面(09-10下施工)
- [资格考试]麦肯锡卓越工作方法读后感
- [资格考试]2007年广西区农村信用社招聘考试试题
- [资格考试]软件实施工程师笔试题
- [资格考试]2014年初三数学复习专练第一章 数与式(
- [资格考试]中国糯玉米汁饮料市场发展概况及投资战
- [资格考试]塑钢门窗安装((专项方案)15)
- [资格考试]初中数学答题卡模板2
- [资格考试]2015-2020年中国效率手册行业市场调查
- [资格考试]华北电力大学学习实践活动领导小组办公
- [资格考试]溃疡性结肠炎研究的新进展
- [资格考试]人教版高中语文1—5册(必修)背诵篇目名
- [资格考试]ISO9001-2018质量管理体系最新版标准
- [资格考试]论文之希尔顿酒店集团进入中国的战略研
- 全国中小学生转学申请表
- 《奇迹暖暖》17-支2文学少女小满(9)公
- 2019-2020学年八年级地理下册 第六章
- 2005年高考试题——英语(天津卷)
- 无纺布耐磨测试方法及标准
- 建筑工程施工劳动力安排计划
- (目录)中国中央空调行业市场深度调研分
- 中国期货价格期限结构模型实证分析
- AutoCAD 2016基础教程第2章 AutoCAD基
- 2014-2015学年西城初三期末数学试题及
- 机械加工工艺基础(完整版)
- 归因理论在管理中的应用[1]0
- 突破瓶颈 实现医院可持续发展
- 2014年南京师范大学商学院决策学招生目
- 现浇箱梁支架预压报告
- Excel_2010函数图表入门与实战
- 人教版新课标初中数学 13.1 轴对称 (
- Visual Basic 6.0程序设计教程电子教案
- 2010北京助理工程师考试复习《建筑施工
- 国外5大医疗互联网模式分析




