Discussion Board

Results 1 to 13 of 13
  1. #1
    Registered User onestepahead's Avatar
    Join Date
    Dec 2005
    Posts
    3
    Hi folks,

    I've been browsing this and also other discussion boards for days in order to find a solution or helpful answer for my following problem. My searches were without success and I hope that some Nokia guy (or any other expert) will read this thread and take care of this.

    I'm developing a J2ME application for my Nokia N70 smartphone (firmware version V 2.0536.0.2 12-09-05 RM-84) an run into trouble opening and using a plain socket connection. The midlet runs very well both on Sun's WTK Wireless Toolkit Version 2.2 and Nokia's Prototype SDK 2.2 for J2ME emulator.

    Here's what I'm doing step by step:

    I'm opening a socket connection to a local web server which runs on a well defined tcp port (say 8888) - other than port 80 in order to bypass midlet signment:
    Code:
    SocketConnection conn = (SocketConnection)Connector.open("socket://server.my.domain:8888");
    After setting some socket options I retrieve the input and output streams associated with that socket:
    Code:
    DataInputStream input = conn.openDataInputStream();
    DataOutputStream output = conn.openDataOutputStream();
    Then I send a simple http request to my server as follows:
    Code:
    String webRequest = "GET / HTTP/1.1\n\n";
    output.write(webRequest.getBytes());
    Having sent the request to the server I simply read from the input stream in a loop:
    Code:
    byte[] inbuf = new byte[512];      
    do {
        int nCount = input.read(inbuf);
        result.append(new String(inbuf, 0, nCount));
    }
    while (input.available() > 0);
    As I said before this application runs well on both Sun's and Nokia's emulators - I'm getting the expected answer from the server. As soon as I run the midlet on the Nokia N70 the application seams to hang right at the statement "input.read()". Using the network protocol analyzer ethereal I can clearly see my request arriving at the web server and the expected result beeing sent to my phone. But the phone does not return from the read() method.

    My provider's GPRS setting are fine since the very same midlet runs properly on my old Siemens S65 cell phone using the very same SIM card. Can anyone (Nokia experts, do you hear me?) explain this weird behaviour? Does anyone out there use socket connections on a Nokia N70 phone?

    Here is the full midlet sample code which opens a TextBox in order to view the result read from the server. Any help is appreciated.

    Code:
    public class SocketMidlet extends MIDlet
    {
      private static final String socketURL = "socket://server.my.domain:8888"; 
      private static final String webRequest = "GET / HTTP/1.1\n\n";
      private TextBox viewer = null;  
    
      public SocketMidlet()   {}
    
      public void pauseApp()  {}
    
      public void startApp()
      {
        viewer = new TextBox("SocketMidlet Test", null, 1024, TextField.ANY );
        viewer.setString("SocketMidlet:");
        Display.getDisplay(this).setCurrent(viewer);
        connect(socketURL);
      }
    
      public void destroyApp(boolean cond)
      {
        notifyDestroyed();
      }
    
      void connect(String url)
      {
        try
        {
          SocketConnection conn = (SocketConnection)Connector.open(url);
          conn.setSocketOption(SocketConnection.LINGER, 5);
          
          DataInputStream input = conn.openDataInputStream();
          DataOutputStream output = conn.openDataOutputStream();
     
          output.write(webRequest.getBytes());
    
          StringBuffer result = new StringBuffer("Content:\n");
    
          byte[] inbuf = new byte[512];      
          do
          {
            int nCount = input.read(inbuf);
            result.append(new String(inbuf, 0, nCount));
          }
          while (input.available() > 0);
    
          viewer.setString(viewer.getString()+result.toString());
    
          output.close();
          input.close();
          conn.close();
        }
        catch (IOException e)
        {
          e.printStackTrace();
        }
        
        Display.getDisplay(this).setCurrent(viewer);
      }
    }
    --
    OneStepAhead

  2. #2
    Registered User springwood64's Avatar
    Join Date
    Oct 2004
    Posts
    17
    Nokia have implemented 'read' as 'readfully'. The method call will terminate when it has read enough bytes to fill your buffer.

    If you are reading HTTP then you will have to read it one byte at a time and cease reading when you detect the end of the response.

  3. #3
    Registered User onestepahead's Avatar
    Join Date
    Dec 2005
    Posts
    3
    Hi springwood64,

    the problem remains even reading byte by byte from a data input stream extracted from a http connection. Look at the following code snipplet. Even that the web server returns a valid HTML response (simple ascii text) the statement "input.available()" return 0 and no data is read at all.

    Code:
    HttpConnection con = null;
    try
    {
      con = (HttpConnection)Connector.open("http://server.my.domain:8888");
    }
    catch (IOException e)
    {
       e.printStackTrace();
    }
    
    DataInputStream input = con.openDataInputStream();
    try 
    {        
      StringBuffer buf = new StringBuffer();
      while (input.available() > 0)  // == 0 (!)
      {
        buf.append((char)input.read());          
      }
      String content = buf.toString();
      int len = content.length();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    input.close();
    Any idea? Thanx.
    --
    OneStepAhead

  4. #4
    Registered User allike's Avatar
    Join Date
    Jan 2006
    Posts
    1
    I met the same problem right now, Waitting for your response!

  5. #5
    Registered User springwood64's Avatar
    Join Date
    Oct 2004
    Posts
    17
    I'm afraid that you cannot rely on available(). Instead you could read one byte at a time - each read will block until a single byte is available:

    Code:
    boolean dataFound = false;
    while(!dataFound)
    {
        byte next = input.read();
        // add 'next' to the data, check if you've got it all and set 'dataFound' to true
    }

  6. #6
    Registered User onestepahead's Avatar
    Join Date
    Dec 2005
    Posts
    3
    Hello, I'm back
    Here is one working solution for the Nokia socket input stream problem:

    1. Use POST, not GET (!)
    2. Read from the input stream chunk by chunk (!!)

    Sample code:

    Code:
       // your URL
       String url = "http://server.my.domain:8888";
       // data buffer (chunk)
       byte[] readBuffer = new byte[8192];
       InputStream input = null;
       HttpConnection trafficConnection = null;
        try
        {
          // open http connection to server and POST request, do NOT use GET as request method
          conn = (HttpConnection)Connector.open(url);      
          conn.setRequestMethod(HttpConnection.POST);
    
          // return if we don't get response code 200 (HTTP OK)
          int rc = conn.getResponseCode();
          if (rc != HttpConnection.HTTP_OK)
          {
            // do something with error code "rc" and then
            return;
          }
          input = conn.openDataInputStream();
        }
        catch (IOException e)
        {
          // HTTP connection failure
          e.printStackTrace();
          return;
        }
    
        try
        {
          // read chunk by chunk
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          int count;
          while ((count = input.read(readBuffer, 0, readBuffer.length)) > 0)
            baos.write(readBuffer, 0, count);
    
          // do something with your data ...
          String result = new String(baos.toByteArray());
        }
        catch (ConnectionNotFoundException e)
        {
          e.printStackTrace();
        }
        catch (IOException e)
        {
          e.printStackTrace();
        }
        finally
        {
          try
          {
            // clean up resources
            if (input != null)
              input.close();
            if (conn != null)
              conn.close();
          }
          catch (IOException e)
          {
          }
        }
    --
    OneStepAhead

  7. #7
    Registered User unugurn's Avatar
    Join Date
    Jan 2006
    Posts
    6
    i am facing same problem with HttpConnection. my phone is Nokia 6630.

    may be this may solve your problem;

    i notice that this happens when http resposnse length is -1. when i changed the code on the server side and set length to appropriate value everyting goes fine on both simulator and phone.

    change your code as follows on

    c = (HttpConnection) Connector.open(url);

    int rc = c.getResponseCode();

    if (rc != HttpConnection.HTTP_OK)
    throw new IOException("HTTP response code: " + rc);

    is = c.openInputStream();

    int length = (int) c.getLength();

    if (length > 0) {

    int actual = 0;
    int bytesread = 0;
    byte[] data = new byte[length];
    while ((bytesread != length) && (actual != -1)) {
    actual = is.read(data, bytesread, length - bytesread);
    bytesread += actual;
    }

    }

  8. #8
    Registered User stevejanko's Avatar
    Join Date
    Aug 2003
    Location
    South Africa
    Posts
    189
    Hi

    I am having a similar problem, and also can not get an answer. One thing I have found as that once the connection is established I press the menu key on the phones keypad, the menu is displayed and then my app receives an answer and the phone switches back to the J2ME app.

    I dont know why the N70 behaves differently to the 6600, 6670 and 6680 on which my app runs perfectly.

    What happens when you press the phone's menu key ?
    Steve

  9. #9
    Registered User juarezjunior's Avatar
    Join Date
    Dec 2005
    Location
    Brazil
    Posts
    1,883
    Hello there,

    You should not rely on the available() method. Check the thread below:

    http://discussion.forum.nokia.com/fo...ad.php?t=76112

    BR

  10. #10
    Registered User stevejanko's Avatar
    Join Date
    Aug 2003
    Location
    South Africa
    Posts
    189
    Thanks BR

    I had already tried this, with the same result, any other ideas

    Regards
    Steve

  11. #11
    Registered User DanielNovy's Avatar
    Join Date
    Jan 2008
    Posts
    3
    Hi guys. Just would like to share the solution to this issue. It's as simple as it looks like: just add a outputstream.flush() command after write. In my Nokia E50 it worked!

    I know it's an very old thread but I search for the solution all over the net and couldn't find it! Hope my post can help other ones.
    Last edited by DanielNovy; 2008-03-07 at 10:27. Reason: Forgot to add more details.

  12. #12
    Registered User huss81's Avatar
    Join Date
    May 2008
    Posts
    12
    Hi,

    Many thanks for posting your solution "myOutputStream.flush". I had the problem for some days that i was able to send data via bluetooth using SPP Probile with "myInputStream.write(..)", but trying to receive some data was always blocking for my application although the other bluetooth device was sending. Thank to your suggestion i now flush the outputstream AFTER writing and BEFORE reading --> everthying works fine :-).

  13. #13
    Registered User djbuan's Avatar
    Join Date
    Dec 2009
    Posts
    1
    Quote Originally Posted by huss81 View Post
    Hi,

    Many thanks for posting your solution "myOutputStream.flush". I had the problem for some days that i was able to send data via bluetooth using SPP Probile with "myInputStream.write(..)", but trying to receive some data was always blocking for my application although the other bluetooth device was sending. Thank to your suggestion i now flush the outputstream AFTER writing and BEFORE reading --> everthying works fine :-).

    This was a pretty good solution! it worked for me as well! Thanks for whoever posted the solution!

Similar Threads

  1. Replies: 2
    Last Post: 2008-04-03, 19:02
  2. Replies: 2
    Last Post: 2007-11-10, 13:19
  3. Pop-Port of Nokia phones
    By HazemRady in forum General Development Questions
    Replies: 0
    Last Post: 2005-10-24, 22:33
  4. SMS and Nokia phones
    By rudolphous in forum Mobile Java General
    Replies: 4
    Last Post: 2003-08-04, 08:50
  5. nokia 7210 modem via IR doesn't work !
    By oussamaaiadi in forum PC Suite API and PC Connectivity SDK
    Replies: 1
    Last Post: 2003-03-06, 10:46

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved