从套接字读取时如何检测客户端何时完成发送请求? [英] While reading from socket how to detect when the client is done sending the request?
问题描述
我现在正在编写一个http服务器,我在从套接字读取时遇到问题。
我的问题是来自客户端的 inputStream
永远不会结束,它一直在读取,直到客户端关闭。
我知道客户端在发送http请求后没有立即关闭与服务器的连接。
当客户端发送了所有请求数据(即标题+正文)时,如何退出 while循环
。
I am now writing a http server and I am having problem with reading from a socket.
My problem is that the inputStream
from the client never ends and it keeps reading until the client is closed.
I know that the client doesn't close the connection with the server immediately after sending the http request.
How can I quit the while loop
when client has sent all the request data (i.e. headers + body).
while (in.hasNextLine()) {
String line = in.nextLine();
if (line.equals("")){ // last line of request header is blank
break; // quit while loop when last line of header is reached
} else {
request = request + line + "\n";
}
}
在阅读了你们的评论和回答后,这是我想出了什么,
After reading comments and answer from you guys, this is what I came up with,
is = incoming.getInputStream();
os = incoming.getOutputStream();
in = new Scanner(is);
out = new DataOutputStream(os);
RequestHandler rh = new RequestHandler();
int length = 0;
while (in.hasNextLine()) {
String line = in.nextLine();
if (line.equals("")) { // last line of request message
// header is a
// blank line
break; // quit while loop when last line of header is
// reached
}
if (line.startsWith("Content-Length: ")) { // get the
// content-length
int index = line.indexOf(':') + 1;
String len = line.substring(index).trim();
length = Integer.parseInt(len);
}
request = request + line + "\n";
}
byte[] body = new byte[length];
int i = 0;
while (i < length) {
byte b = in.nextByte();
body[i] = b;
i++;
}
但是,我仍然不知道按字节读取数据。我可以编写我的代码来读取-1,但是当没有EOF且客户端没有关闭连接时仍然卡住。
but, I still don't get it about reading by bytes. I can write my code to read until -1, but still stuck when there is no EOF and client is not closing connection.
推荐答案
我已经知道了:)谢谢大家的评论和答案...
I've got it :) Thank you guys for comments and answers...
is = incoming.getInputStream(); // initiating inputStream
os = incoming.getOutputStream(); // initiating outputStream
in = new BufferedReader(new InputStreamReader(is)); // initiating
// bufferReader
out = new DataOutputStream(os); // initiating DataOutputSteream
RequestHandler rh = new RequestHandler(); // create a
// requestHandler
// object
String line;
while ((line = in.readLine()) != null) {
if (line.equals("")) { // last line of request message
// header is a
// blank line (\r\n\r\n)
break; // quit while loop when last line of header is
// reached
}
// checking line if it has information about Content-Length
// weather it has message body or not
if (line.startsWith("Content-Length: ")) { // get the
// content-length
int index = line.indexOf(':') + 1;
String len = line.substring(index).trim();
length = Integer.parseInt(len);
}
request.append(line + "\n"); // append the request
} // end of while to read headers
// if there is Message body, go in to this loop
if (length > 0) {
int read;
while ((read = in.read()) != -1) {
body.append((char) read);
if (body.length() == length)
break;
}
}
request.append(body); // adding the body to request
这篇关于从套接字读取时如何检测客户端何时完成发送请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!