HttpServer, Request, Response
Java代码

package com.iteye.wely.server;  import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;  /** * Created by shenhongxi on 16/3/21. */
public class HttpServer {  public static final String WEB_ROOT = System.getProperty("user.dir") + File.separator + "ixhong-tomcat-web/src/main/webapp";  private static final String SHUTDOWN_COMMAND = "SHUTDOWN";  private boolean shutdown = false;  public static void main(String[] args) {  HttpServer server = new HttpServer();  System.out.println(WEB_ROOT);  server.await();  }  private void await() {  ServerSocket serverSocket = null;  int port = 8080;  try {  serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"));  System.out.println("Server started!");  } catch (IOException e) {  e.printStackTrace();  System.exit(1);  }  while (!shutdown) {  Socket socket = null;  InputStream input = null;  OutputStream output = null;  try {  socket = serverSocket.accept();  input = socket.getInputStream();  output = socket.getOutputStream();  Request request = new Request(input);  request.parse();  Response response = new Response(output);  response.setRequest(request);  response.sendStaticResource();  socket.close();  shutdown = request.getUri().equals(SHUTDOWN_COMMAND);  } catch (Exception e) {  e.printStackTrace();  continue;  }  }  }
}
Java代码  收藏代码
package com.iteye.wely.server;  import java.io.IOException;
import java.io.InputStream;  /** * Created by shenhongxi on 16/3/21. */
public class Request {  private InputStream input;  private String uri; // 性能考虑,用byte[]  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 (IOException e) {  e.printStackTrace();  i = -1;  }  for (int j = 0; j < i; j++) {  request.append((char) buffer[j]);  }  System.out.println(request.toString());  uri = parseUri(request.toString());  }  private String parseUri(String requestStr) {  // GET /index.html HTTP/1.1  // Accept: text/plain; text/html  // ...  int index1 = requestStr.indexOf(' ');  int index2;  if (index1 != -1) {  index2 = requestStr.indexOf(' ', index1 + 1);  if (index2 > index1) {  return requestStr.substring(index1 + 1, index2);  }  }  return null;  }  public String getUri() {  return uri;  }
}
Java代码  收藏代码
package com.iteye.wely.server;  import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;  /** * Created by shenhongxi on 16/3/21. */
public class Response {  private static final int BUFFER_SIZE = 1024;  Request request;  OutputStream output;  public void sendStaticResource() throws IOException {  byte[] bytes = new byte[BUFFER_SIZE];  FileInputStream fis = null;  try {  File file = new File(HttpServer.WEB_ROOT, request.getUri());  if (file.exists()) {  fis = new FileInputStream(file);  int ch = fis.read(bytes, 0, BUFFER_SIZE);  while (ch != -1) {  output.write(bytes, 0, ch);  ch = fis.read(bytes, 0, BUFFER_SIZE);  }  } else {  String errorMsg = "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>";  output.write(errorMsg.getBytes());  }  } catch (Exception e) {  e.printStackTrace();  } finally {  if (fis != null) {  fis.close();  }  }  }  public Response(OutputStream output) {  this.output = output;  }  public void setRequest(Request request) {  this.request = request;  }
}

原文链接:[]

一个简单的Web服务器相关推荐

  1. ipad php mysql_如何用PHP/MySQL为 iOS App 写一个简单的web服务器(译) PART1

    原文:http://www.raywenderlich.com/2941/how-to-write-a-simple-phpmysql-web-service-for-an-ios-app 作为一个i ...

  2. 我的Go语言学习之旅八:创建一个简单的WEB服务器

    因为一直在做WEB程序,所以更关注WEB界的发展,这里就用GO做了一个简单的WEB服务器,直接看例子吧 package main import ( "fmt" "net/ ...

  3. ios php mysql实例_如何用PHP/MySQL为 iOS App 写一个简单的web服务器(译) PART1

    原文:http://www.raywenderlich.com/2941/how-to-write-a-simple-phpmysql-web-service-for-an-ios-app 作为一个i ...

  4. 用python写一个简单的web服务器

    人生苦短,我用python 简洁高效,这才是理想的语言啊 分享一点python的学习经验-----如何用python写一个简单的web服务器 首先,我们需要简单地了解一下网络通信协议,这里用白话介绍一 ...

  5. web服务器python_一个简单的web服务器(python)

    今天用python写一个简单的web服务器代码网上都有只是为了方便大家学习做了一个简单的教程 第一首先我们来一张架构以及运行过程的流程图 本文学习仅供参考,需要更多资料可以加群:496257369 简 ...

  6. java实现web服务器_Java 实现一个简单的web服务器

    在日常的开发中,我们用过很多开源的web服务器,例如tomcat.apache等等.现在我们自己实现一个简单的web服务器,基本的功能就是用户点击要访问的资源,服务器将资源发送到客户端的浏览器.web ...

  7. 如何用PHP/MySQL为 iOS App 写一个简单的web服务器(译) PART1

    原文:http://www.raywenderlich.com/2941/how-to-write-a-simple-phpmysql-web-service-for-an-ios-app 作为一个i ...

  8. 【计算机网络作业】Java UDP聊天 和 Socket编写一个简单的Web服务器

    1-1 假设Tom和Jerry利用Java UDP进行聊天,请为他们编写程序.具体如下: (1).Tom和Jerry聊天的双方都应该具有发送端和接收端: (2).利用DatagramSocket与Da ...

  9. 深入理解计算机系统:网络编程 下 一个简单的web服务器

    编写一个web服务器的基础是熟悉套接字接口.笔者因为还没有对其进行实践,只对其有一些粗浅的了解.本文重点记录一个web服务的核心功能主要由哪几部分组成. 1. main函数 之前写到,从客户端到服务器 ...

  10. rust自创服务器_用Rust写了一个简单的Web服务器

    Rust 最近学了一阵Rust,这个语言的目的是系统编程,卖点是无GC的内存安全.为了实现这一点,Rust引入了所有权.借用.生命周期的概念.可以在编译器检查出可能的内存问题,如野指针.局部变量指针等 ...

最新文章

  1. python和c运行速度的对比实验_Python中单线程、多线程和多进程的效率对比实验...
  2. 关于Oracle数据库19c中的关键字和保留字的说明
  3. Ubuntu18.04 安装 gnuplot
  4. 快速下载Spring官网下载dist.zip中所有jar,例如spring-5.2.10.RELEASE-dist.zip
  5. 查看表扫描次数,并对比索引对表查询的作用
  6. Android Studio 在res中新建文件夹不显示
  7. 大班体育游戏 电子计算机,【大班户外游戏】_幼儿园大班体育游戏活动设计40篇...
  8. linux能运行安卓模拟器吗,Ubuntu 14.04中使用模拟器运行Android系统
  9. 电子表格控件Spreadsheet 对象方法事件详细介绍
  10. Vmware 15 安装 win7 虚拟机 (初学者操作与详解教程)
  11. 【欢迎白嫖】新·vbs表白代码
  12. 天涯明月刀最新服务器,天涯明月刀最新开服时间表 | 手游网游页游攻略大全
  13. Visual FoxPro操作dbf时语句过长换行问题
  14. 小程序毕业设计 基于java后台微信在线考试小程序毕业设计参考
  15. nginx配置简单图片显示
  16. 手把手教你用Python轻松玩转SQL注入
  17. NOIP2016·洛谷·天天爱跑步
  18. 王者荣耀小游戏4.0
  19. 元宇宙时代NFT的价值衡量
  20. 牛客小白月赛B JAVA大数或String

热门文章

  1. 2017《面向对象程序设计》课程作业五
  2. 利用纯CSS3实现超立体的3D图片侧翻倾斜效果
  3. Mac 调整磁盘分区:调整本地与虚拟机内存分区占比
  4. (十二)RabbitMQ消息队列-性能测试
  5. 软工课后作业01 15100152
  6. TimePickerDialog -下划线颜色修改
  7. 测试的主要评测方法(3)
  8. 中国诚信全球垫底?讲讲《Science》现在的论文有多不靠谱
  9. 峰情万种_Enhancement of Peak Visual
  10. R语言数据可视化---交互式图表recharts包