Join Our 3 MILLION+ Members Today! Register Here | Login
Go Back   BlackBerry Forums at CrackBerry.com > BlackBerry Professionals > App Developers

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
    Thread Author   #1  
Old 04-23-2009, 02:28 PM
CrackBerry Abuser
Device(s): 9700 (Bold)
Carrier: AT&T/Cingular
Pin: 21C3344D
 
Location: Ames, IA
Join Date: Feb 2009
Posts: 256
Likes Received: 0
Thanked 0 Times in 0 Posts
Default DataInputStream read() does not receive

So I'm writing an application that uses the network. For the "server" device I am connecting to the protocol is a request response so I created a TcpClient class that connects to a given IP/Port and then has a send method that sends the request and receives the response which is then returned. I am currently running this only on the JDE w/ the simulator as I don't have the server device on the internet yet.

The problem I'm having is the request is sent (I see it in Wireshark doing a network capture) and a response is issued back from the server (again, see it on wireshark). I issue a DataInputStream.read(byte[]) and my application locks up and I do not receive any data. Here is the code for this function.

Code:
  MyTcpClient(String client, int port)
  {
    String args = new String("socket://" + client + ":" + Integer.toString(port) + ";deviceside=true");
        
    try
    {
      socket = (SocketConnection)Connector.open(args, Connector.READ_WRITE, true);
      
    }
    catch (IOException e)
    {
      System.out.println(e);
    }
    
    is = null;
    os = null;
  }

  public boolean connect()
  {
    boolean ret = false;
    try
    {
      is = socket.openDataInputStream();
      os = socket.openDataOutputStream();
      ret = true;
    }
    catch (IOException e)
    {
      System.out.println(e);
    }
    
    return ret;
  }

  public byte[] send(byte[] data, int size)
  {
    byte ret[] = new byte[1024];
    
    if (is != null && os != null && socket != null)
    {
      try
      {          
        os.write(data, 0, size);
        //os.flush();
        int len = 0;
        while (-1 != (len = is.read(ret)))
        {
          System.out.println("Byte's received: " + String.valueOf(len));
        }
        System.out.println("Howdy");
      }
      catch (IOException e)
      {
        System.out.println(e);
      }
    }
    
    return ret;
  }
The response coming back is around 200 bytes so I should be able to get it in one receive and pass it back. The code stops on the "while( -1 !=...." line. Any ideas?
__________________
BlackBerry Addict in Iowa
PIN: 21C3344D
www.twitter.com/jeffparent
Reply With Quote Tip this Post
  #2  
Old 04-23-2009, 05:50 PM
CrackBerry Abuser
Device(s): 9000 (Bold)
Carrier: Vodafone
 
Join Date: Oct 2008
Posts: 494
Likes Received: 0
Thanked 1 Time in 1 Post
Default

You're not doing this right, I think. read(byte []) returns -1 *when the end of stream is reached*. So, you're doing a read and the stream ends in the one call to read. Check/print your buffer outside the loop.

If I'm not correct, what you can try and do is write a proper Java app with the same socket logic, to ensure you're doing the right thing, and run it from the command line.
Reply With Quote Tip this Post
Reply
BlackBerry Forums at CrackBerry.com > > BlackBerry Professionals > App Developers   DataInputStream read() does not receive

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes