通过ServerSocket实现简单web服务器

利用Java Socket编写一个简单的Web服务器,具体如下:

(1)、使用 ServerSocket 监听某一端口,然后等待连接获取 Socket对象;

(2)、创建一个类 HttpServer 继承 java.lang.Thread 类,重写 run()方法,执行浏览器请求;

(3)、获得浏览器请求,解析资源文件路径;

(4)、读取资源文件,响应给浏览器;

(5)、浏览器地址栏输入: http://localhost:8000/index.html

源码

package com;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class webserver {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(8000);
            while(true){
                //服务器套接字
                Socket socket = serverSocket.accept();
                new HttpServer(socket).start(); {
                };
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
package com;

import java.io.*;
import java.net.Socket;

public class HttpServer extends Thread {
    private Socket socket;
    public HttpServer(Socket socket) {
        this.socket=socket;
    }

    @Override
    public void run() {
        super.run();
        String filePath = "/index.html";
        File file=new File(System.getProperty("user.dir")+filePath);
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            System.out.println("reader  :" + reader);
            StringBuffer sb = new StringBuffer();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\r\n");
            }
            StringBuffer result = new StringBuffer();
            result.append("HTTP /1.1 200 ok \r\n");
            result.append("Content-Type:text/html \r\n");
            result.append("Content-Length:" + file.length() + "\r\n");
            result.append("\r\n" + sb.toString());
            OutputStream out=socket.getOutputStream();
            out.write(result.toString().getBytes());
            System.out.println(result.toString());
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

 

负载平衡,MQTT协议支持,修正大量Bug,最新稳定版 Delphi7/XE6/XE7/XE8 ******************************************************* sgcWebSockets ******************************************************* [*] : Bug [+] : New [-] : Deleted [/] : Breaking changes Versions -------- 4.1.0: 2017 April [+] : Added support for Rad Studio 10.2 Tokyo. [+] : Added support for Linux compiler. [+] : MQTT client, added subscribe method. [+] : MQTT client, added unsubscribe method. [+] : MQTT client, added publish method. [+] : MQTT client, added Authentication property, allows to set user and password to authenticate against MQTT Server. [+] : MQTT client, added HeartBeat property, keeps alive connection. [+] : MQTT client, added LastWillTestament property, when client disconnects, sends a message to other connected clients. [+] : New Property "NotifyDeletes" in Dataset Server Protocol, if enabled (by default) broadcast deleted record to all clients. [+] : New Method "BroadcastRecord" in Dataset Server Protocol, sends dataset record values to all clients. [+] : New Method "MetaData" in Dataset Server Protocol, sends metadata info to a single client. [+] : New Method "Synchronize" in Dataset Server Protocol, sends dataset record values to a single client. [*] : Fixed Bug ReadTimeout and ConnectionTimeout in client component. [*] : Added Guid property to Client File Protocol. [*] : Fixed Bug Invalid Character when trying to access to built-in javascript libraries. [*] : Fixed Bug File Protocol when BufferSize was set to zero, file was not saved properly. [*] : Fixed Memory Leaks on NextGen compiler. [*] : Fixed WebRTC Chrome console errors.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值