×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    14

    Question J2ME Http Server by using ServerSocketConnection

    I am developing Midlet Client-Midlet Server application. Here is my codes. On the client side, client sends the request and reads the response from the server. Server can read the request and prepares the response. However, client doesn't wait for the response, and try to get response earlier. So, While reading the data input stream on the client side, I am getting "IOException Response Empty".
    What should I send to be able to send a HTTP response to the client from server side?(Other than "car")

    Code:
    //Client
    HttpConnection hc = null;
    InputStream dis = null;
    OutputStream dos = null;
    int rc;
    String requeststring = "This";
    String message=null;
    try {
    hc = (HttpConnection) Connector.open(urlstring, Connector.READ_WRITE);
    hc.setRequestMethod(HttpConnection.POST);
    hc.setRequestProperty("User-Agent","Profile/MIDP-1.0 Confirguration/CLDC-1.0");
    hc.setRequestProperty("Accept_Language", "en-US");
    hc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    dos = hc.openOutputStream();
    dos.write(requeststring.getBytes());

    //Response from server
    dis = new DataInputStream(hc.openInputStream());
    int ch;
    while ((ch = is.read()) != -1) {
    message = message + (char) ch;
    }

    } catch (IOException ioe) {
    ioe.printStackTrace();
    }

    //Server
    ServerSocketConnection s = (ServerSocketConnection) Connector.open("socket://:" +
    MYECHOPORT);
    SocketConnection sc = (SocketConnection) s.acceptAndOpen();
    DataInputStream din = incoming.openDataInputStream();
    DataOutputStream dout = incoming.openDataOutputStream();
    boolean done = false;
    while (!done) {
    StringBuffer str = new StringBuffer();
    byte[] b = new byte[1000];
    int byteLength = din.read(b);
    for (int i = 0; i < byteLength; i++) {
    byte c = b[i];
    str.append((char) c);
    }
    if (str.length() == 0) {
    done = true;
    break;
    } else {
    try {
    String trial="car";
    dout.write(trial.getBytes());
    } catch (Exception e) {
    e.printStackTrace();
    }
    break;
    }
    }
    din.close();
    dout.close();
    incoming.close();

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: J2ME Http Server by using ServerSocketConnection

    You will need to process the header sent in the request and generate a valid response.

    Read the HTTP 1.1 specification. It will tell the format of the request sent by the client, and the format of response you must create.

    You will have a hard time writing an HTTP server without thorough understanding the protocol.

    Graham.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    14

    Question Re: J2ME Http Server by using ServerSocketConnection

    Thank you very much Graham.

    Quote Originally Posted by grahamhughes View Post
    You will need to process the header sent in the request and generate a valid response.

    Read the HTTP 1.1 specification. It will tell the format of the request sent by the client, and the format of response you must create.

    Graham.
    Is there anyone who knows how to create a valid HTTP response through SocketConnection?
    Thanks in advance.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    14

    Re: J2ME Http Server by using ServerSocketConnection

    Please, please, please, Help me immediately.

Similar Threads

  1. [Announce]SWFxME(open-source) extend Flash Lite Using J2ME (Http Server)
    By sajisoft in forum [Archived] Flash Lite on Nokia Devices
    Replies: 1
    Last Post: 2009-10-08, 08:53
  2. Replies: 1
    Last Post: 2008-02-20, 11:05
  3. http and session management in j2me.
    By qasitouch in forum Mobile Java Networking & Messaging & Security
    Replies: 4
    Last Post: 2007-05-22, 14:31
  4. client server programming in j2me
    By mottaisami in forum Mobile Java Networking & Messaging & Security
    Replies: 2
    Last Post: 2006-09-05, 14:43
  5. 6630 S60: Browser is unable to connect to local http server
    By schulze.holger in forum Mobile Java Networking & Messaging & Security
    Replies: 0
    Last Post: 2006-05-24, 06:37

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×