Go Back   BlackBerry Forums at CrackBerry.com > BlackBerry Professionals > Developer's Forum

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 04-23-2009, 03:28 PM
jeff.parent's Avatar
CrackBerry Abuser
Device Model: 8310 (Curve)
Carrier: AT&T/Cingular
PIN: 209b3a24
 
Join Date: Feb 2009
Location: Ames, IA
Posts: 167
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: 209B3A24
www.twitter.com/jeffparent
Reply With Quote
  #2  
Old 04-23-2009, 06:50 PM
CrackBerry Abuser
Device Model: 9000 (Bold)
Carrier: Vodafone
 
Join Date: Oct 2008
Posts: 489
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
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



 
 Site Support | Accessory Order Support | App Store Support | Advertise | Newsletter | About Us

Creating smartphone communities
Android Central - Android reviews, news and forums Crackberry - Blackberry news, reviews and community TiPb - iPhone news, accessory reviews & forums
Pre Central - Palm Pre Review, News and Community Treo Central - Treo & Centro News and Forums WMExperts - Windows Mobile Reviews & News

The names RIM and BlackBerry are registered Trademarks of Research in Motion Limited.
CrackBerry.com is in No Way Affiliated with Research in Motion Limited.
Copyright ©2007-2009 Smartphone Experts. Terms and Conditions. Privacy Policy. All rights reserved.