Sometimes the close() can cause problems for the remote server, since
it yanks the connection out. There's no particular guarantee that the
server will have time to process the entire request and send you the
results.

Also, on some systems, closing the output stream can close the socket
too, so you may never be able to read from it if you do this. I would
strongly suggest moving the close to the end.

Alex.

On 23/06/06, Andrew Zhang <[EMAIL PROTECTED]> wrote:
Tim,

Thank you very much.  I revised the test case a little according to your
suggestion, but RI still fails sometimes.
Would you please review the test?  The error message is: expected <400> but
was: <200>
Thanks a lot in advance!


    public void test_SocketChannel_BlockWriteRead() throws IOException {
        final int CAPACITY_NORMAL = 200;
        InetSocketAddress localAddr1 = new InetSocketAddress("127.0.0.1
",1234);
        ServerSocket server = new ServerSocket(1234);

        SocketChannel channel = SocketChannel.open();
        channel.connect(localAddr1);
        Socket serverSocket = server.accept();

        OutputStream out = serverSocket.getOutputStream();
        byte[] sendBuf = new byte[CAPACITY_NORMAL * 2];
        for (int i = 0; i < CAPACITY_NORMAL * 2; i++) {
            sendBuf[i] = (byte) i;
        }
        // send CAPACITY_NORMAL * 2 bytes data
        out.write(sendBuf);
        out.flush();
        out.close();

        ByteBuffer buf1 = ByteBuffer.allocate(CAPACITY_NORMAL);
        ByteBuffer buf2 = ByteBuffer.allocate(CAPACITY_NORMAL);
        ByteBuffer[] buf ={buf1, buf2};

        // should receive CAPACITY_NORMAL * 2 bytes data
        long count = 0;
        do{
            long ret = channel.read(buf);
            if(ret == -1){
                break;
            }
            count += ret;
        }while(count < CAPACITY_NORMAL*2);
        assertEquals(CAPACITY_NORMAL * 2, count);
    }

Best regards,

On 6/23/06, Tim Ellison <[EMAIL PROTECTED]> wrote:
>
> Andrew,
>
> There is no guarantee that the out.flush() will actually send the bytes
> (it will just ensure that they get from Java to the OS).  Indeed, there
> is no guaranteed way to force the TCP stack to send the bytes; but you
> can try by reducing the socket send buffer size (SO_SNDBUF) and setting
> no delay on (TCP_NODELAY).  The best bet is to close the output stream
> socket when you have finished writing, and read until you get a -1 on
> the client.
>
> Regards,
> Tim
>
> Andrew Zhang wrote:
> > Hi everybody,
> >
> > It seems RI Socket.getOutputStream().write(byte[])  doesn't send all
> data
> > sometimes. Consider following test, which often blocks on RI:
> >
> >    public void test_SocketChannel_BlockWriteRead() throws IOException {
> >        final int CAPACITY_NORMAL = 200;
> >        InetSocketAddress localAddr1 = new InetSocketAddress("127.0.0.1
> > ",1234);
> >        ServerSocket server = new ServerSocket(1234);
> >
> >        SocketChannel channel = SocketChannel.open();
> >        channel.connect(localAddr1);
> >        Socket serverSocket = server.accept();
> >
> >        OutputStream out = serverSocket.getOutputStream();
> >        byte[] sendBuf = new byte[CAPACITY_NORMAL * 2];
> >        for (int i = 0; i < CAPACITY_NORMAL * 2; i++) {
> >            sendBuf[i] = (byte) i;
> >        }
> >        // send CAPACITY_NORMAL * 2 bytes data
> >        out.write(sendBuf);
> >        out.flush();
> >
> >        ByteBuffer buf1 = ByteBuffer.allocate(CAPACITY_NORMAL);
> >        ByteBuffer buf2 = ByteBuffer.allocate(CAPACITY_NORMAL);
> >        ByteBuffer[] buf ={buf1, buf2};
> >
> >        // should receive CAPACITY_NORMAL * 2 bytes data
> >        // RI often hangs here, with CAPACITY_NORMAL bytes data received.
> >        long count = 0;
> >        do{
> >            count += channel.read(buf);
> >        }while(count < CAPACITY_NORMAL*2);
> >        assertEquals(CAPACITY_NORMAL * 2, count);
> >    }
> >
> > I think it's a bug of RI. Am I missing something? Please correct me if
> I'm
> > wrong. There're also some similar tests in NIO module with FIXME mark.
> >
> > If it's a bug of RI, I'll raise a JIRA to tidy up those "FIXME" in NIO.
> >
> > Thanks!
> >
> > Best regards,
> >
>
> --
>
> Tim Ellison ([EMAIL PROTECTED])
> IBM Java technology centre, UK.
>
> ---------------------------------------------------------------------
> Terms of use : http://incubator.apache.org/harmony/mailing.html
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>


--
Andrew Zhang
China Software Development Lab, IBM



---------------------------------------------------------------------
Terms of use : http://incubator.apache.org/harmony/mailing.html
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to