客户端代码“随机” 每当尝试创建 GzipInputstream 时都会失败
我有一台基本上可以满足多个客户端需求的服务器。 我基本上使用 gzip(输入/输出)流来压缩客户端-服务器之间的数据。
许多客户端可以同时向服务器发送请求,因此我有一个线程来满足每个客户端的需求。
现在,我遇到的问题是,每当与服务器的连接建立后尝试执行以下操作时,“随机”一些客户端代码就会失败。
GZIPInputStream in = new GZIPInputStream(server.getInputStream());
我得到java.io.EOFException
。
当我说随机时,我的意思是我在异常中找不到任何模式。 请求正在正确发送(否则它将无法处理任何客户端请求)。
我搜索了很多..但找不到任何东西..:(
关于上述问题的任何指针?
Socket connection= new Socket("localhost",2428);
GZIPOutputStream out = new GZIPOutputStream(connection.getOutputStream());
out.write(url.getBytes());
out.finish();
GZIPInputStream in=null;
try {
in = new GZIPInputStream(connection.getInputStream(),1024); // Exception raised here
} catch(Exception e) { }
接受新连接并生成新线程的服务器代码。
ServerSocket dsWeb= new ServerSocket(2428);
Socket webClient;
while(true){
webClient = dsWeb.accept();
executor.execute(new ThreadPool()); // each request to be handled by a separate thread
线程内的代码..
GZIPInputStream inWeb = new GZIPInputStream(webClient.getInputStream());
int c1=0;
byte[] b1 = new byte[100000];
c1=inWeb.read(b1);
//Process the request
GZIPOutputStream outWeb = new GZIPOutputStream(webClient.getOutputStream());
outWeb.write(/* Response */);
outWeb.finish();
这是异常的堆栈跟踪我得到:
java.io.EOFException at java.util.zip.GZIPInputStream.readUByte(Unknown Source)
at java.util.zip.GZIPInputStream.readUShort(Unknown Source)
at java.util.zip.GZIPInputStream.readHeader(Unknown Source)
at java.util.zip.GZIPInputStream.<init>(Unknown Source)
at java.util.zip.GZIPInputStream.<init>(Unknown Source)
at com.WebServerVNCRequest.doGet(WebServerVNCRequest.java:78)
I have a server that basically caters to multiple clients.
I am basically using gzip(input/output)stream to compress the data between client-server.
Many clients can send the requests to server at the same time and hence I have a thread to cater to each client.
Now, the problem that I am experiencing is that "randomly" some client code fails whenever it tries to execute following after the connection to the server has been established.
GZIPInputStream in = new GZIPInputStream(server.getInputStream());
I get java.io.EOFException
.
And when I say randomly, I mean there is no pattern that I can find in the exception.
The requests are being send properly (else it would not work for any client requests).
I have searched a lot..but couldn't find anything.. :(
Any pointers on the above problem?
Socket connection= new Socket("localhost",2428);
GZIPOutputStream out = new GZIPOutputStream(connection.getOutputStream());
out.write(url.getBytes());
out.finish();
GZIPInputStream in=null;
try {
in = new GZIPInputStream(connection.getInputStream(),1024); // Exception raised here
} catch(Exception e) { }
Server code that accepts a new connection and spawns a new thread.
ServerSocket dsWeb= new ServerSocket(2428);
Socket webClient;
while(true){
webClient = dsWeb.accept();
executor.execute(new ThreadPool()); // each request to be handled by a separate thread
The code within the thread ..
GZIPInputStream inWeb = new GZIPInputStream(webClient.getInputStream());
int c1=0;
byte[] b1 = new byte[100000];
c1=inWeb.read(b1);
//Process the request
GZIPOutputStream outWeb = new GZIPOutputStream(webClient.getOutputStream());
outWeb.write(/* Response */);
outWeb.finish();
Here's the stacktrace of the exception I get:
java.io.EOFException at java.util.zip.GZIPInputStream.readUByte(Unknown Source)
at java.util.zip.GZIPInputStream.readUShort(Unknown Source)
at java.util.zip.GZIPInputStream.readHeader(Unknown Source)
at java.util.zip.GZIPInputStream.<init>(Unknown Source)
at java.util.zip.GZIPInputStream.<init>(Unknown Source)
at com.WebServerVNCRequest.doGet(WebServerVNCRequest.java:78)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误率是多少? 这些是否是随机(偶然)的网络错误? 或者您的服务器偶尔会遇到内部错误?
听起来问题并不在于 GZip 数据本身,而在于偶尔的网络中断。 我的猜测是您的服务器偶尔会生成异常并发送无效流,从而在客户端上导致此类错误。 也许尝试记录服务器上的所有异常,以确保不会偶尔出现内部故障?
What is the error rate? Could it be that these are random (occasional) network errors? Or perhaps your server is occasionally experiencing an internal error?
It doesn't really sound like the problem is with the GZip data itself, but rather with an occasional network break. My guess is that your server occasionally generates an exception and sends an invalid stream, causing this type of error on the client. Perhaps try logging all exceptions on the server to insure that you aren't occasionally getting internal failure there?
问题是
webClient
未传递给new ThreadPool()
。 这只能意味着它是该类的实例成员,这会引入线程安全问题。 将其设为局部变量并将其显式传递给处理它的 Runnable。请注意,您阅读的代码过于乐观。 您不会在一次读取中获得整个请求。 你必须循环。 100Kb 的缓冲区大小只是浪费空间:即使在解压缩之后,单次读取也无法接近该大小。
The problem is that
webClient
isn't passed tonew ThreadPool()
. This can only mean that it is an instance member of the class, which introduces a thread-safety problem. Make it a local variable and pass it explicitly to theRunnable
that handles it.NB your reading code is unduly optimistic. You won't get the entire request Ina single read. You have to loop. A buffer size of 100Kb is just wasted space: you won't get anywhere near that in a single read, even after dezipping.