Java字符串在打印前为空,但在此之前不为空

3

我想读取一些HTTP请求,并且有以下内容:

public class HTTPServerThread extends Thread {
private Socket socket = null;
private BufferedOutputStream out;

public HTTPServerThread(Socket socket) {
    super("HTTPServerThread");
    this.socket = socket;
}

public void run() {
    try {
        out = new BufferedOutputStream(socket.getOutputStream());
        BufferedReader in = new BufferedReader(new InputStreamReader(new DataInputStream(socket.getInputStream())));

        String request = "";
        String temp;
        while ((temp = in.readLine()) != null) {
            request += temp+"\n";
            System.out.println("request:\n"+request);
        }
        System.out.println("request final:\n"+request);
        if (request.equals("")) System.out.println("Request empty");

        out.close();
        socket.close();

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

当我运行我的代码并在服务器请求某些内容时,在while循环中请求会被分段打印出来,所以一切正常。但是当我最终将其打印出来时,它给了我"",因此打印出"请求为空",我不知道我的(可能很愚蠢的)错误在哪里,请帮助我:)

当前输出:

request:
GET / HTTP/1.1

request:
GET / HTTP/1.1
Host: localhost

request:
GET / HTTP/1.1
Host: localhost
Connection: keep-alive

request:
GET / HTTP/1.1
Host: localhost
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

request:
GET / HTTP/1.1
Host: localhost
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.0 Safari/537.36

request:
GET / HTTP/1.1
Host: localhost
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.0 Safari/537.36
Accept-Encoding: gzip,deflate,sdch

request:
GET / HTTP/1.1
Host: localhost
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.0 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: de,en-US;q=0.8,en;q=0.6

request:
GET / HTTP/1.1
Host: localhost
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.0 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: de,en-US;q=0.8,en;q=0.6


request final:

Request empty

在主程序中使用类:

public class HTTPServer {

public static void main(String[] args) throws IOException {
    ServerSocket serverSocket = null;
    int port;
    boolean listening = true;

    //read port from args[0] if exists, else port = 80

    if (args.length == 0) {
        port = 80;
    }
    else {
        port = Integer.parseInt(args[0]);
    }

    //start listening on port

    try {
        serverSocket = new ServerSocket(port);
    }
    catch (IOException e) {
        System.out.println("Could not listen on port: "+port);
        System.exit(-1);
    }

    System.out.println("Java HTTP-Server");
    System.out.println("Port: " + port);

    //start new Thread if someone wants to connect

    while (listening)
        new HTTPServerThread(serverSocket.accept()).start();

    serverSocket.close();
}

2
请使用 StringBuilder 进行字符串连接,仅供参考! - AllTooSir
2
你能发布当前的输出吗? - Jon Onstott
1
@JonathanDrapeau:请求似乎是一个局部变量,而不是在线程之间共享的字段。 - JB Nizet
1
你可能有两个线程并行运行。第一个线程打印它接收到的前七行,然后在它的循环中被阻塞,因为服务器不再发送任何内容;第二个线程从服务器接收到一个空响应,并打印出它的空响应。将当前线程添加到日志语句中以确认或证实。 - JB Nizet
1
如果是这种情况,将您的停止条件修改为 while ((temp = in.readLine()) != null && temp.length() > 0) 应该可以解除线程卡住的问题。 - i Code 4 Food
显示剩余19条评论
2个回答

1
正如JB Nizet所猜测的那样,问题是由原始线程在while循环中被卡住,而第二个线程实际上打印出空字符串引起的。 while循环的最后一次迭代会导致一个空的但不是null的temp字符串,可以从似乎重复的请求输出中看到,右侧有一个额外的空行,就在线程被卡住之前。
因此,将停止条件更改为
while ((temp = in.readLine()) != null && temp.length() > 0) {

这将允许原始线程退出循环并打印正确的最终请求。


-3

你需要在socket中指定IP地址和端口号

否则,socket不知道要连接到哪里

可以使用以下代码:

Socket socket = new Socket(int ip, int port);


1
-1 帖子,+1 @Arthur 我认为 OP 已经从他的套接字接收到数据,所以我们可以假设它已经正确设置了。 - TecHunter
这与问题有什么关系?如果您愿意,可以添加注释等内容。 - Darius X.
我没有任何问题接收任何东西,直接将第一行读入请求并打印出来也可以工作... - philipp94831

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接