2

My code is structured into three classes Coordinator, Sender and Receiver. Coordinator takes a size from user and informs sender about it sender. Then sender first tells this size to receiver and then sends that much data to receiver. Once receiver receives the whole size then it informs coordinator about the size received. Once this cycle completes, coordinator waits for another size input from user.

Whole system works well for size = 1K, 32K, even till 64K. However when I increase the size further the problem is that receiver stops receiving after a certain size. In most of the case it is 86800 Bytes. It just gets stuck there, gets no more data. Sender sends the whole data, connections are intact(I checked using isConnected() API). Any idea why this is not working

Following is code at 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);
            }

Following is code at Receiver: /Makes connection with coordinator and with sender/

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

Following is my Utils code:

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', ' ' );
    }
PHcoDer
  • 1,166
  • 10
  • 23

0 Answers0