开发者

Java socket programming - stream get stuck

开发者 https://www.devze.com 2022-12-21 23:11 出处:网络
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 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 = inStrea开发者_运维问答m.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.


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.


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


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消