[英]datainputstream missing bytes in.read(b)
I'm trying to write a program which acts as a server that will read bytes from a client that is written in PHP - sends request via socket (which i cannot recode due to policy) Here is the server code: The server runs in: Red Hat Enterprise Linux Server release 6.2 (Santiago) 我正在尝试编写一个充当服务器的程序,该程序将从用PHP编写的客户端读取字节-通过套接字发送请求(由于策略我无法重新编码)这是服务器代码:服务器在以下位置运行:红帽企业Linux服务器版本6.2(圣地亚哥)
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!");
}
I encountered a problem when the client sends request consisting of 4948 bytes. 当客户端发送包含4948个字节的请求时,我遇到了一个问题。 The only bytes the server can read is 2090. Another thing that seems a mystery to me is that, when I run the server via Netbeans in my local (which is a Windows 7 Pro), it works as expected.
服务器只能读取2090个字节。对我来说,另一件事似乎是个谜,那就是,当我在本地(Windows 7 Pro)中通过Netbeans运行服务器时,它可以按预期运行。 I dont know what is wrong.
我不知道怎么了。 Please help.. :)
请帮忙.. :)
Thanks! 谢谢!
read()
method isn't guaranteed to fill the buffer. read()
方法会填充缓冲区。 Therefore if you don't receive the expected number of bytes in a single read, you have to loop until you do receive them. 因此,如果您在一次读取中没有收到预期的字节数,则必须循环直到收到它们。
readFully()
would have worked if the buffer size agreed with the size of what was sent. 如果缓冲区大小与发送的大小一致,则
readFully()
将起作用。 In your case you specified a buffer of 10,000 bytes, which weren't sent, so it would have blocked waiting for the other 10000-4948 bytes. 在您的情况下,您指定了10,000个字节的缓冲区,但该缓冲区未发送,因此它会阻塞等待其他10000-4948个字节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.