0 Replies Latest reply: Oct 15, 2010 5:16 AM by 805714 RSS

    SecureConnection in Java ME 3.0 problem

    805714
      I'm trying to use SecureConnection to do a simple query, using www.google.com port 443 and sending a simple GET request. My test app code is at the end of this post. The behaviour I observe is that after the socket is open if I allow the thread to get to the blocking read() call then the write call will block and not return (writing no data). If I change the code so that the write happens before the thread starts then it works ok.

      This works fine on WTK 2.5.2 on Windows and NOT on Java ME 3.0 SDK on Mac.

      I realise that this code is not ideal for HTTP, I'm using this just as an example. With sockets I should be able to do a blocking read() and call write() as a choose and it works on WTK Windows as I said above.

      Any insights would be appreciated.


      package com.test;

      import java.io.DataInputStream;
      import java.io.DataOutputStream;

      import javax.microedition.io.Connector;
      import javax.microedition.io.SecureConnection;
      import javax.microedition.midlet.MIDlet;
      import javax.microedition.midlet.MIDletStateChangeException;

      public class RGMidlet extends MIDlet implements Runnable {

           public RGMidlet() {
           }
           protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
           }
           protected void pauseApp() {
           }

           SecureConnection sock;
           DataInputStream in;
           DataOutputStream out;

           protected void startApp() throws MIDletStateChangeException {
                Thread t = new Thread(this);
                try {
                     sock = (SecureConnection) Connector
                               .open("ssl://www.google.com:443");
                     in = sock.openDataInputStream();
                     out = sock.openDataOutputStream();

                     // start read thread now that socket is open
                     t.start();

                     // sleep to allow read thread to get into position
                     Thread.sleep(5000);

                     System.out.println("about to write");
                     out.write("GET / HTTP/1.1\n\n".getBytes());
                     System.out.println("it wrote!!");
                } catch (Exception ex) {
                     t.interrupt();
                }
           }

           public void run() {
                try {
                     int numread = 0;
                     System.out.println("run thread up");

                     while (numread >= 0) {
                          byte[] buffer = new byte[10 * 1024];
                          numread = in.read(buffer);
                          String str = new String(buffer, 0, numread, "UTF-8");
                          System.out.println("read data=" + str);
                     }
                } catch (Exception ex) {
                     ex.printStackTrace();
                }
           }
      }