Java datainputstream在.read(b)中缺少字节
我正在尝试编写一个程序,该程序充当服务器,从用PHP编写的客户端读取字节-通过套接字发送请求(由于策略原因,我无法重新编码)。以下是服务器代码: 服务器运行于:Red Hat Enterprise Linux server 6.2版(圣地亚哥) 当客户端发送由4948字节组成的请求时,我遇到了一个问题。服务器只能读取2090字节。 另一件对我来说似乎是个谜的事情是,当我在本地(Windows 7 Pro)通过Netbeans运行服务器时,它会按预期工作。我不知道怎么了。请帮忙……) 谢谢Java datainputstream在.read(b)中缺少字节,java,byte,datainputstream,Java,Byte,Datainputstream,我正在尝试编写一个程序,该程序充当服务器,从用PHP编写的客户端读取字节-通过套接字发送请求(由于策略原因,我无法重新编码)。以下是服务器代码: 服务器运行于:Red Hat Enterprise Linux server 6.2版(圣地亚哥) 当客户端发送由4948字节组成的请求时,我遇到了一个问题。服务器只能读取2090字节。 另一件对我来说似乎是个谜的事情是,当我在本地(Windows 7 Pro)通过Netbeans运行服务器时,它会按预期工作。我不知道怎么了。请帮忙……) 谢谢 TCP
read()
方法不能保证填充缓冲区readfull()。在您的例子中,您指定了一个10000字节的缓冲区,该缓冲区没有发送,因此它会阻止等待其他10000-4948字节
public void run() {
try {
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(0);
while(!isInterrupted) {
try {
Socket server = serverSocket.accept();
LOG.info("Request received from : " + server.getRemoteSocketAddress());
DataInputStream in = new DataInputStream(server.getInputStream());
// DataInputStream in = new DataInputStream(
// new BufferedInputStream(server.getInputStream(), 10000));
byte[] bytes = new byte[10000];
int byteDupLength = in.read(t_bytes);
// in.readFully(bytes); // I tried this but to no avail
// int byteDupLength = bytes.length;
LOG.info(byteDupLength);
byte[] byteDup = new byte[byteDupLength];
System.arraycopy(bytes, 4, byteDup, 0, byteDupLength);
// FOR INFORMATION ONLY
/*for (byte b : byteDup){
LOG.info(b);
}*/
ByteBuffer buffer = ByteBuffer.wrap(byteDup);
LOG.info(buffer);
forwardRequest(byteDup);
server.close();
}
catch(SocketTimeoutException s) {
LOG.error("Socket timed out!", s);
break;
}
catch(IOException e)
{
LOG.error("IOException:", e);
break;
}
}
}
catch (IOException ex) {
LOG.error("Server socket is null", ex);
}
LOG.fatal("ReceiverEngine interrupted!");
}