最MC论坛

标题: [Java]简单的HTTP服务器 [打印本页]

作者: Jianghao7172    时间: 2017-5-23 18:53
标题: [Java]简单的HTTP服务器
之前看到我女票发了个Java的HTTP服务器,但是我发现有个问题,访问一次以后就会卡死。。学了一段时间的Java,我也写了一个HTTP服务器
不多说了,放上代码
  1. package com.niconicocraft.web;

  2. import java.io.*;
  3. import java.net.ServerSocket;
  4. import java.net.Socket;

  5. public class WebSystem extends Thread {

  6.         private byte[] content;
  7.         private byte[] header;
  8.         private int port = 80;
  9.         public static ServerSocket server;
  10.         public OutputStream out;
  11.         public InputStream in;

  12.         public WebSystem(int Port) throws UnsupportedEncodingException {
  13.                 this.content = "".getBytes();//data;
  14.                 this.port = 80; //端口
  15.         }

  16.         public void run() {
  17.                 try {
  18.                         server = new ServerSocket(this.port);
  19.                         System.out.println("服务器运行于端口: " + server.getLocalPort());

  20.                         while (true) {
  21.                                 Socket connection = null;
  22.                                 try {
  23.                                         connection = server.accept();
  24.                                         out = new BufferedOutputStream(connection.getOutputStream());
  25.                                         in = new BufferedInputStream(connection.getInputStream());

  26.                                         StringBuffer request = new StringBuffer();
  27.                                         while (true) {
  28.                                                 int c = in.read();
  29.                                                 if (c == '\r' || c == '\n' || c == -1) {
  30.                                                         break;
  31.                                                 }
  32.                                                 request.append((char) c);
  33.                                         }
  34.                                         if (request.indexOf("GET / ") != -1) {
  35.                                                 String WebString ="<h2>Niconico Craft Web System</h2>";
  36.                                                 showPage(WebString);
  37.                                                 System.out.println(request);
  38.                                         } else {
  39.                                                 String RequestSource = request.substring(5, request.indexOf(" HTTP/"));
  40.                                                 String WebString;
  41.                                                 String header;

  42.                                         }
  43.                                 } catch (IOException e) {
  44.                                         // TODO: handle exception
  45.                                 } finally {
  46.                                         if (connection != null) {
  47.                                                 connection.close();
  48.                                         }
  49.                                 }
  50.                         }
  51.                 } catch (IOException e) {
  52.                         System.err.println("无法启动HTTP服务器,端口被占用。");
  53.                 }
  54.         }
  55.         public static void stopserver() throws IOException {
  56.                 server.close();
  57.         }
  58.         public void showPage(String str) throws UnsupportedEncodingException, IOException {
  59.                 String WebString = str;
  60.                 String header = "HTTP/1.0 200 OK\r\n"
  61.                         + "Server: Niconico Craft WebServer 1.2\r\n"
  62.                         + "Content-length: " + WebString.getBytes().length + "\r\n"
  63.                         + "Content-type: text/html\r\n\r\n";
  64.                 out.write(header.getBytes("UTF-8"));
  65.                 out.write(WebString.getBytes());
  66.                 out.flush();
  67.         }
  68. }
复制代码
@wx302x  QWQ



作者: Summer大大    时间: 2017-5-24 09:37
所以说有什么用呢?
作者: lss233    时间: 2017-5-24 21:17
嘛...只能接受一个连接的http服务器
不过有一个可以一起写代码的对象我还是羡慕的
羡慕一脸.jpg
作者: V乐乐    时间: 2017-5-25 12:03
Nice...
额,只能有一个连接吗





欢迎光临 最MC论坛 (http://www.zuimc.com/) Powered by Discuz! X3.2