简单的Java网络IO无法处理大数据

简单的Java网络IO无法处理大数据

我遇到了一个奇怪的问题,它不会出现在MACOS上,而只会出现在Linux上...
当我在linux中运行以下代码时,有时会收到不一致的消息。
该代码仅创建了两个线程,其中一个向另一个发送了很多全为1的字节数组,另一个线程仅检查了所有内容是否为1。它偶尔会收到0(似乎取决于n ..)。

有人可以帮我吗?

    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。

关于java - 简单的Java网络IO无法处理大数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24117089/

10-09 00:09