Java套接字编程-流被卡住

发布于 2024-08-24 00:55:53 字数 555 浏览 3 评论 0原文

我目前正在开发一个简单的代理服务器,它接收来自浏览器的http请求,处理它,然后将其转发到所需的Web服务器。

我尝试从浏览器连接的套接字的输入流中获取请求,除了流在接收到最后一个数据块后卡住之外,一切都很好。

我的代码其实很简单,如下所示:

ServerSocket servSocket = new ServerSocket(8282);  
Socket workSocket = servSocket.accept();  
InputStream inStream = workSocket.getInputStream();  
byte[] buffer = new byte[1024];  
int numberRead = 0;  

while ((numberRead = inStream.read(buffer, 0, 1024)) != -1){  
System.out.println(new String(buffer));  
}  

即使请求接收完成,循环也无法退出。

有什么方法可以解决这个问题吗?

预先感谢您的任何建议。

I am currently working on a simple proxy server, which receives http request from browser, process it, then forward it to the desire web server.

I try to get the request from the input stream of the socket connected by the browser, everything is fine except that the stream get stuck after receiving the last block of data.

My code is in fact very simple, as shown below:

ServerSocket servSocket = new ServerSocket(8282);  
Socket workSocket = servSocket.accept();  
InputStream inStream = workSocket.getInputStream();  
byte[] buffer = new byte[1024];  
int numberRead = 0;  

while ((numberRead = inStream.read(buffer, 0, 1024)) != -1){  
System.out.println(new String(buffer));  
}  

The loop simply cannot exit, even the request reception is finished.

Is there any method to workaround this problem?

Thanks in advance for any advice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

紫﹏色ふ单纯 2024-08-31 00:55:53

InputStream javadoc 该方法将阻塞,直到数据可用或遇到 EOF。因此,Socket 的另一端需要关闭它 - 然后 inStream.read() 调用将返回。

另一种方法是先发送您要读取的消息的大小,这样您就可以提前知道必须读取多少字节。或者您可以使用 BufferedReader 以逐行方式从套接字读取。 BufferedReader 有一个 readLine() 方法,每次读取一行时都会返回,这应该适合您,因为 HTTP 协议包很好地分为几行。

As in InputStream javadoc the method will block until the data is available or the EOF is encountered. So, the other side of Socket needs to close it - then the inStream.read() call will return.

Another method is to send the size of message you want to read first, so you know ahead how many bytes you have to read. Or you can use BufferedReader to read from socket in line-wise way. BufferedReader has a method readLine() which returns every time a line is read, which should work for you as HTTP protocol packages are nice divided into lines.

一抹淡然 2024-08-31 00:55:53

它将循环直到连接关闭,并且客户端可能正在等待您的 HTTP 响应并且不会关闭它。

It will cycle until the connection is closed, and the client is probably waiting for HTTP response from you and doesn't close it.

辞慾 2024-08-31 00:55:53

浏览器在关闭连接之前正在等待响应。
另一方面,您的读取方法将阻塞,直到流/连接关闭或收到新数据。

The browser is waiting for a response before it closes the connection.
Your read-method on the other hand will block until the stream/connection is closed or new data is received.

魔法唧唧 2024-08-31 00:55:53

根据您当前的代码,这不是直接的解决方案。

由于 HTTP 是基于行的协议,您可能需要使用 Buffered Reader 并对其调用 readLine()

Not a direct solution according to your current code.

As HTTP is a line based protocol, you might want to use a Buffered Reader and call readLine() on it.

甜中书 2024-08-31 00:55:53

当一个 http 请求到来时,它总是以一个空行结束,例如:

GET /someFile.html HTTP/1.1
Host: www.asdf.com

发送该请求后,客户端连接将等待服务器的响应,然后再关闭连接。因此,如果您想解析来自用户的请求,您最好使用 BufferedReader 并读取整行,直到到达空白行的文本行。

The when a http request comes in it will always be concluded with a blank line, for example:

GET /someFile.html HTTP/1.1
Host: www.asdf.com

After sending that request the client connection will then wait for a response from the server before closing the connection. So if you want to parse the request from the user you are probably better off using a BufferedReader and reading full lines until you reach a lines of text that is blank line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文