Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

BufferedInputStream problem in Sockets in Java

Options
  • 14-10-2004 11:24am
    #1
    Registered Users 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