Balaji
Balaji

Reputation: 285

socket inputstream issue in j2me (BB)

I am working on XMPP client for BB with JXA, but JXA api takes time to read from inputstream and through timeout error after 2 mins. I written seperate socket program in J2ME and executed in the BB simulator, it could exchange XML but the inputstream is not quitting from the while loop, gets hang in reading. Please see the below code..

class SocketThread extends Thread
{
    public void run()
    {

          ConnectionFactory connFact = new ConnectionFactory();
          ConnectionDescriptor connDesc;
          connDesc = connFact.getConnection("socket://xxxxxx.p1.im:5222");
          if (connDesc != null)
          {
              SocketConnection httpConn;
              httpConn = (SocketConnection)connDesc.getConnection();
              try
              {
                 InputStream is = httpConn.openInputStream();
                 OutputStream os = httpConn.openOutputStream();

                 String a = "<?xml version=\"1.0\"?><stream:stream to=\"xxxxx.p1.im\" xml:lang=\"en\" xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\" version=\"1.0\">";

                 os.write(a.getBytes());

                 byte[] b = new byte[1024];
                 int len =0;
                            while ((len = is.read(b)) > 0) {


                                String str = new String(b);

                                 System.out.println("Server n: " + str);


                                 }

                  UiApplication.getUiApplication().invokeLater(new Runnable()
                  {
                      public void run()
                      {
                          Dialog.alert("got response xml stream..: "  
                                        );
                      }
                   });
               } 
               catch (IOException e) 
               {
                 System.err.println("Caught IOException: " 
                      + e.getMessage());
               }
          }
    }
}

Above program not coming out of the loop unless until it timesout, please help me out to fix this issue to move forward.

Thanks in advance.

Upvotes: 1

Views: 757

Answers (1)

user207421
user207421

Reputation: 310850

A read timeout means no data arrived within the timeout period. Nothing else.

Your code is wrong in other ways however: you are assuming the read filled the buffer, and ignoring positive values of 'len' when constructing the String, so you are passing yourself junk.

Upvotes: 1

Related Questions