簡體   English   中英

多線程的Java Web服務器

[英]multithreaded java web server

我正在嘗試實現一個多線程的java webserver。

這是我的主要內容:

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();


        }
    }
}

這是我的線程類:

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);
        }
    }
}

通過邏輯,對於服務器獲得的每個請求,它將創建一個新線程。 每個線程都與特定請求相關聯。 我的問題是,當我請求文件(例如index.html)時,服務器獲取請求,但文件未加載,瀏覽器繼續加載。

我發現每個線程都已啟動但它沒有完成。

這是一個輸出:

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

我究竟做錯了什么? 有沒有更好的方法? 請注意,我只做了一個線程來測試來自一個IP的請求,並且將在不再一次構建時解決這個問題。

您永遠不會編寫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);

如果要實現自己的服務器,則應閱讀RFC 2616

如果要使用瀏覽器連接到服務器,則必須返回帶有標頭的HTTP響應。 是一個簡單的HTTP服務器示例。 或者更好地看看這個已回答的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM