我的代码以某一行结束

时间:2017-10-30 11:55:26

标签: java

我正在练习我的网络Java技能,我创建了一个连接到用户端口号指定的服务器Java类:

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

public class WebServerMain {
    public WebServerMain(int port){
        try{
            String content = " ";
            String userChoice = " ";
            ServerSocket ss = new ServerSocket(port);
            System.out.println("Server: Initialised a socket at port " + 
port);
           Socket conn = ss.accept();
            System.out.println("Server: Awating for a client");
            InputStreamReader ir = new 
InputStreamReader(conn.getInputStream());
            PrintWriter out = new PrintWriter(conn.getOutputStream(),true);
            BufferedReader userIn = new BufferedReader(ir);
            userChoice = userIn.readLine();
            System.out.println("Server: The choice we got is: " + 
userChoice);

            System.out.println("Server: User have chosen GET Method");
            BufferedReader in = new BufferedReader(new 
FileReader("test.html"));
            String str;
            System.out.println("All good here");
            while ((str = in.readLine()) != null) {
                content += str;
            }
            in.close();
            System.out.println("Server: The content is "  + content);

            out.println(content);

        } catch(IOException e){
            e.getMessage();
        }
    }

   public static void main(String[] args){
        String path;
        int port;
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter the port number");
        port = in.nextInt();
        WebServerMain server = new WebServerMain(port);
    }
}

输出如下: enter image description here

似乎代码退出befire新的BufferReader读取html文件并输出到客户端。

我选择了Safari浏览器并通过同一端口连接到本地主机。 还请注意,BufferedReader的文件名是硬编码的,只是为了测试它,我想我需要使用正则表达式删除GET才能正确读取文件。

1 个答案:

答案 0 :(得分:0)

public WebServerMain(int port) {
    try {
        String content = " ";
        String userChoice = " ";
        ServerSocket ss = new ServerSocket(port);
        System.out.println("Server: Initialised a socket at port " + port);
        Socket conn = ss.accept();
        System.out.println("Server: Awating for a client");
        InputStreamReader ir = new InputStreamReader(conn.getInputStream());
        PrintWriter out = new PrintWriter(conn.getOutputStream(), true);
        BufferedReader userIn = new BufferedReader(ir);
        userChoice = userIn.readLine();
        System.out.println("Server: The choice we got is: " + userChoice);

        System.out.println("Server: User have chosen GET Method");
        BufferedReader in = new BufferedReader(new FileReader(
                //"C:/Users/jayaprakash.j/Desktop/1.html"));// 
                userChoice.split(" ")[1]));
        String str;
        System.out.println("All good here");
        while ((str = in.readLine()) != null) {
            content += str;
        }
        in.close();
        System.out.println("Server: The content is " + content);

        out.println("HTTP/1.1 200 OK");
        out.println("Content-Type: text/html");
        out.println("Content-Length: " + content.length());
        out.println();
        out.println(content);
        out.close();
        conn.close();

    } catch (IOException e) {
        e.getMessage();
    }
}