简体   繁体   English

多线程的Java Web服务器

[英]multithreaded java web server

I am trying to implement a multi threaded java webserver. 我正在尝试实现一个多线程的java webserver。

Here is my main: 这是我的主要内容:

import java.net.*;

public class Main {
    public static void main(String argv[]) throws Exception{

        ServerSocket welcomeSocket = new ServerSocket(6790);
        while(true){
            System.out.println("Waiting...");
            Socket cSock = welcomeSocket.accept();
            System.out.println("Accepted connection : " + cSock);

            Server a = new Server(cSock);
            a.start();


        }
    }
}

here is my thread class: 这是我的线程类:

import java.io.*;
import java.net.*;
import java.util.Scanner;


public class Server extends Thread{
    Socket cSock;

    Server(Socket cSock){   //constructor
        this.cSock = cSock;
    }

    public void run(){
        try{
            String request;
            Scanner inFromClient = new Scanner(cSock.getInputStream());
            DataOutputStream outToClient = new DataOutputStream(cSock.getOutputStream());
            request = inFromClient.nextLine();
            System.out.println("Received: "+request);

            //trimming URL to extract file name
            String reqMeth = request.substring(0, 3);
            String reqURL = request.substring(5, (request.lastIndexOf("HTTP/1.1")));
            String reqProto = request.substring(request.indexOf("HTTP/1.1"));
            System.out.println("Request Method:\t" +reqMeth +"\nRequest URL:\t" +reqURL+ "\nRequest Protocol: " +reqProto);

            //passing file name to open
            File localFile = new File(reqURL.trim());
            byte [] mybytearray  = new byte [(int)localFile.length()];
            FileInputStream fis = new FileInputStream(localFile);
            BufferedInputStream bis = new BufferedInputStream(fis);
            bis.read(mybytearray,0,mybytearray.length);

            //sending file to stream
            System.out.println("Sending...");           
            outToClient.write(mybytearray,0,mybytearray.length);
            outToClient.flush();
            outToClient.close();

        }catch(Exception e){
            System.out.println(e);
        }
    }
}

By logic, with each request the server gets, it will create a new thread. 通过逻辑,对于服务器获得的每个请求,它将创建一个新线程。 Each thread is associated with a particular request. 每个线程都与特定请求相关联。 My problem is when i request for a file (eg. index.html), the server gets the request, but the file does not load, the browser keeps on loading. 我的问题是,当我请求文件(例如index.html)时,服务器获取请求,但文件未加载,浏览器继续加载。

i figured out that each thread is started but it does not complete. 我发现每个线程都已启动但它没有完成。

here is an output: 这是一个输出:

Waiting...
Accepted connection : Socket[addr=/192.168.0.10,port=58957,localport=6790]
Waiting...
Accepted connection : Socket[addr=/192.168.0.10,port=58958,localport=6790]
Waiting...
Received: GET /html/index.html HTTP/1.1
Request Method: GET
Request URL:    html/index.html 
Request Protocol: HTTP/1.1
Accepted connection : Socket[addr=/192.168.0.10,port=59093,localport=6790]
Waiting...
Received: GET /index.html HTTP/1.1
Request Method: GET
Request URL:    index.html 
Request Protocol: HTTP/1.1

what am i doing wrong? 我究竟做错了什么? and is there any better way? 有没有更好的方法? note that i did only one thread to test request from only one IP, and will build on than once this is solved. 请注意,我只做了一个线程来测试来自一个IP的请求,并且将在不再一次构建时解决这个问题。

You are never writing the HTTP headers. 您永远不会编写HTTP标头。

outToClient.write("HTTP/1.0 200 OK\r\n");
outToClient.write("Connection: Close\r\n");
outToClient.write("\r\n");
outToClient.write(mybytearray,0,mybytearray.length);

If you are going to implement your own server, you should read RFC 2616 . 如果要实现自己的服务器,则应阅读RFC 2616

If you want to connect to the server with a browser, it must return HTTP response with headers. 如果要使用浏览器连接到服务器,则必须返回带有标头的HTTP响应。 Here is an simple example of HTTP server. 是一个简单的HTTP服务器示例。 Or better look at this answered question. 或者更好地看看这个已回答的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM