Java非阻塞SocketChannel在一定大小后没有收到

时间:2016-08-26 07:51:18

标签: java nonblocking socketchannel

我的代码分为三个类Coordinator,Sender和Receiver。协调员从用户那里获取一个大小,并通知发件人它的发件人。然后发送者首先告诉接收者这个大小,然后将那么多数据发送给接收者。一旦接收器接收到整个大小,它就会通知协调器接收的大小。一旦此循环完成,协调器将等待来自用户的另一个大小输入。

整个系统适用于尺寸= 1K,32K,甚至64K。然而,当我进一步增加尺寸时,问题是接收器在一定尺寸后停止接收。在大多数情况下,它是86800字节。它只是卡在那里,没有更多的数据。发送者发送整个数据,连接完好(我使用isConnected()API检查)。知道为什么这不起作用

以下是Sender的代码:

/*Makes connection with coordinator and opens a socket for receiver to connect here*/
 while (true) {
                int size = Utils.receiveInt(coordinator);
                Utils.sendInt(size, receiver);
                Utils.sendTill(size, receiver, null);
            }

以下是Receiver的代码:       / 与协调员和发件人建立连接 /

while(true){
            int size = Utils.receiveInt(sender);
            Utils.receiveTill(size, sender);
            Utils.sendInt(size, coordinator);
        }

以下是我的Utils代码:

public static boolean sendInt (int num, SocketChannel channel) throws IOException{
        return sendTill(10, channel, intTo10CharString(num));
    }

    public static boolean sendTill (int size, SocketChannel channel, String dataU) throws IOException{
        String data;
        if(dataU == null){
            data = arbitString(size);
        }else{
            data = dataU;
        }
        CharBuffer buffer = CharBuffer.wrap(data);
        while(buffer.hasRemaining()){
            channel.write(Charset.defaultCharset().encode(buffer));
            System.out.println("bufPos: "+buffer.position()+" bufLim: "+buffer.limit());
        }
        return true;
    }

    public static int receiveInt(SocketChannel channel) throws NumberFormatException, IOException{
        return Integer.parseInt(new String(Arrays.copyOf(Charset.defaultCharset().decode(receiveTill(intStringLength, channel)).array(),intStringLength)));
    }

    public static ByteBuffer receiveTill(int size, SocketChannel channel) throws IOException{
        int left = size;
        int ret;
        System.out.println("going to make buffer");
        ByteBuffer buffer = ByteBuffer.allocate(size);
        System.out.println("bufPos: "+buffer.position()+" bufLim: "+buffer.limit()+" bufferCap: "+buffer.capacity());
        while(left>0){
            ret = channel.read(buffer);
            if(ret < 0){
                System.err.println("error in reading");
            }else if (ret!=0){
                //System.out.println("Received: "+ret+" left: "+left);
                System.out.println("bufPos: "+buffer.position()+" bufLim: "+buffer.limit());//+" bufferCap: "+buffer.capacity());
            }else if(ret == 0){
                if(!channel.isConnected()){
                    System.out.println("not connected");
                }
            }
            left -= ret;
        }
        buffer.flip();
        return buffer;
    }

    public static String intTo10CharString(int integer){
        return String.format("%010d",integer);
    }

    public static String arbitString(int size) {
        return CharBuffer.allocate(size).toString().replace( '\0', ' ' );
    }

0 个答案:

没有答案