Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

BufferedInputStream problem in Sockets in Java

  • 14-10-2004 10:24AM
    #1
    Registered Users, Registered Users 2 Posts: 53 ✭✭


    I written a client and Server program. However Messages are not being recieved 10% of the time by the Server and the client says it sends them. Neither client or Server throw exceptions. and the server seems to be blocking on read as normal.

    What i have done iswritten my own class that extends the BufferedInputStream class to allow an array of bytes to be read, and will block until all the array is collected.

    /**
    * Reads next bytes into an array of bytes. It blocks until all requested
    * bytes are read. s is the socket.
    *
    * @param b the buffer into which the data is read.
    * @param off the start offset of the data in b.
    * @param len the number of bytes to read.
    *
    * @return the number of bytes read.
    *
    * @exception IOException if an I/O error occurs or if the end connection has been closed.
    */
    private int read(byte b[], int off, int len) throws IOException {
    int r = 0;
    int p = 0;

    while (p < len) {
    r = super.read(b, p+off, len-p);

    System.out.println("got -> " + b);
    if (r == -1) {
    if (s != null) {
    s.setSoTimeout(0);
    }
    throw new IOException("end of stream reached");
    }
    p += r;
    // Change to socket timeout after the first bytes are read
    if (s != null) {
    s.setSoTimeout(10000);
    }
    }
    if (s != null) {
    s.setSoTimeout(0);
    }
    return len;
    }

    (Sorry if the indentation is out)

    However when entend FilterInputStream, instead of BufferedInputStream, all messages are recieved.

    Can anyone explain what is going on?


Advertisement