简体   繁体   English

datainputstream in.read(b)中缺少字节

[英]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! 谢谢!

  1. TCP is a byte stream protocol. TCP是字节流协议。
  2. The 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.

相关问题 Java IO中的“ while(-1!=(len = in.read(b)))”和“ while((len = in.read(b))> 0)”之间有什么区别? - What is difference between “while (-1 != (len = in.read(b)))” and “while ((len = in.read(b)) > 0)” in Java IO? 使用dataoutputstream将字节写入套接字时,如何读取datainputstream? - How to read a datainputstream when using a dataoutputstream to write bytes to socket? Java的DataInputStream.readUTF和读取的字节数 - Java's DataInputStream.readUTF and number of bytes read InputStream in.read()的行为与预期不同 - InputStream in.read() behaving differently than expected DataInputStream将通过顺序读取覆盖字节吗 - Will DataInputStream override bytes with sequential reads 如何通过使用DataInputStream类指定偏移量和字节长度将字节读取到数组中? - How to read bytes into arrays by specifying offset and byte length using the DataInputStream class? 使用DataInputStream从TCP套接字读取的字节之前的不需要的nul字符 - Unwanted nul characters preceding bytes from TCP socket read using DataInputStream 使用DataInputStream读取小字节长度时读取性能降低 - Slow read performance using DataInputStream when it comes to reading small length of bytes DataInputStream读取未阻塞 - DataInputStream read not blocking DataInputStream.read() 与 DataInputStream.readFully() - DataInputStream.read() vs DataInputStream.readFully()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM