我有一个基本上可以满足多个客户的服务器。我基本上使用 gzip(input/output)stream 来压缩客户端-服务器之间的数据。
许多客户端可以同时向服务器发送请求,因此我有一个线程来满足每个客户端的需求。
现在,我遇到的问题是,在建立与服务器的连接后,每当尝试执行以下操作时,某些客户端代码“随机”失败。
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)