简单Java网络IO不适用于大数据

简单Java网络IO不适用于大数据,java,io,network-programming,Java,Io,Network Programming,我遇到了一个奇怪的问题,它不会出现在MACOS上,而只出现在linux上。。。 在linux中运行以下代码时,有时会收到不一致的消息。 代码只创建了两个线程,其中一个线程向另一个线程发送大量包含所有1的字节数组,另一个线程只检查所有内容是否都是1。它偶尔会发生,这取决于。。它收到0 有人能帮我吗 import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket;

我遇到了一个奇怪的问题,它不会出现在MACOS上,而只出现在linux上。。。 在linux中运行以下代码时,有时会收到不一致的消息。 代码只创建了两个线程,其中一个线程向另一个线程发送大量包含所有1的字节数组,另一个线程只检查所有内容是否都是1。它偶尔会发生,这取决于。。它收到0

有人能帮我吗

    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;

    public class TestIO {
        static final int n = 500000;
        final static byte d  = 10;

        static class SenderRunnable implements Runnable {
            SenderRunnable () {}
            private ServerSocket sock;
            protected OutputStream os;

            public void run() {
                try {
                    sock = new ServerSocket(12345);            // create socket and bind to port
                    Socket clientSock = sock.accept();                   // wait for client to connect
                    os = clientSock.getOutputStream();  

                    byte[] a = new byte[10];
                    for(int i = 0; i < 10; ++i)
                        a[i] = d;

                    for (int i = 0; i < n; i++) {
                        os.write(a);
                    }
                    os.flush();

                    sock.close(); 
                } catch (Exception e) {
                    e.printStackTrace();
                    System.exit(1);
                }
            }
        }

        static class ReceiverRunnable implements Runnable {
            ReceiverRunnable () {}
            private Socket sock;
            public InputStream is;
            public void run() {
                try {
                    sock = new java.net.Socket("localhost", 12345);          // create socket and connect
                    is = sock.getInputStream();

                    for (int i = 0; i < n; i++) {
                        byte[] temp = new byte[10];
                        is.read(temp);
                        for(int j = 0; j < 10; ++j)
                            if(temp[j] != d){
                                System.out.println("weird!"+" "+i+" "+j+" "+temp[j]);
                            }
                    }
                    sock.close(); 
                } catch (Exception e) {
                    e.printStackTrace();
                    System.exit(1);
                }
            }
        }


        public static void test1Case() throws Exception {
            SenderRunnable sender = new SenderRunnable();
            ReceiverRunnable receiver = new ReceiverRunnable();
            Thread tSnd = new Thread(sender);
            Thread tRcv = new Thread(receiver);
            tSnd.start(); 
            tRcv.start(); 
            tSnd.join();
            tRcv.join();
        }

        public static void main(String[] args)throws Exception {
            test1Case();
            test1Case();
            test1Case();
            test1Case();
            test1Case();
            test1Case();
            test1Case();
            test1Case();
        }
    }
谢谢!我将部分代码更改为下面的代码,现在就可以工作了

    for (int i = 0; i < n; i++) {
        byte[] temp = new byte[10];
        int remain = 10;
        remain -= is.read(temp);
        while(0 != remain)
        {
            remain -= is.read(temp, 10-remain, remain);
        }
        for(int j = 0; j < 10; ++j)
            if(temp[j] != d){
                System.out.println("weird!"+" "+i+" "+j+" "+temp[j]);
            }
    }

你的错误在读者方面。
方法读取不保证填充数组。它只接收已经可用的字节,并返回已经获得的字节数。因此,您必须在循环中读取字节,直到read返回-1。

您应该在调用InputStream.read时检查实际读取的字节数。

更好的方法是,使用DataInputStream.readFully,它已经做到了这一点。查看DataInputStream的实现。readFully:它实现了我描述的模式。所以,使用它是可以的。您还可以使用ApacheIOutils中的各种实用程序