×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    17

    Read from socket never blocking

    We have an application that consists of three threads: one for the GUI, one that listens on a SocketConnection and one that every now and then sends messages out on the same SocketConnection.

    The read thread calls read(buffer, offset, length) which I would expect to either return some data or block, as the connection is always open.

    Instead the read never blocks. It either returns data or returns a -1 length. If I loop on the read statement then it will continue returning -1 until some more data arrives and will then return that. It is almost as if the -1 is saying there is no data available. Eventually after receiving a few messages it will then throw an exception as if it has lost the connection.

    I have tried the same code using a single byte read() call and the behaviour is the same.

    Can anyone see where I am going wrong here? Is it simply impossible to hold open a SocketConnection and receive messages on it every now and then? Could it be that sending messages on the same socket is somehow getting in the way of the reads?

  2. #2
    Super Contributor
    Join Date
    Dec 2005
    Location
    Europe/Poland/Warsaw
    Posts
    1,697

    Re: Read from socket never blocking

    hi,

    can you compare your code with e.g. sample chat midlet posted here ("Network examples")?
    http://itp.nyu.edu/~dbo3/cgi-bin/Cla...HelloCellPhone
    (midlet+socket)
    regards,
    Peter

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    17

    Re: Read from socket never blocking

    Hmmm, it seems very similar to the way I am doing things.

    The difference is that a -1 is handled as a closed connection. But I seem to get -1 returned from my read calls when ever there is no data in the buffer.

    I will study the code in more detail to see if I can see any other differences.

    Thanks for the reply!

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    17

    Re: Read from socket never blocking

    To add to the fun it appears this problem is very much device specific. Reading a byte at a time works fine on Series 60 phones, but does not work at all on Series 40 ones.

    If I read in chunks of bytes then it works for a little while on Series 40 but then throws an exception as if the socket was closed.

Similar Threads

  1. Socket read on Nokia 6600 blocks forever
    By delipsingh in forum Mobile Java Networking & Messaging & Security
    Replies: 9
    Last Post: 2010-04-13, 15:00
  2. socket read hangs until socket is closed
    By camroe in forum Mobile Java Networking & Messaging & Security
    Replies: 1
    Last Post: 2005-07-21, 09:44
  3. Inputstream read method is blocking
    By gnhansen in forum Bluetooth Technology
    Replies: 0
    Last Post: 2004-09-29, 14:44
  4. Bluetooth L2CAP Socket: Read() vs. Recv()
    By jamespinakis in forum Symbian
    Replies: 2
    Last Post: 2004-06-11, 00:44
  5. Bluetooth L2CAP Socket: Read() vs. Recv()
    By jamespinakis in forum Bluetooth Technology
    Replies: 1
    Last Post: 2003-06-10, 22:52

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×