>
Make a SOCKET connection from a J2ME Program
Welcome, Guest

by Pradeep Sundriyal on Monday, 11 May 2009
Make a SOCKET connection from a J2ME Program




This J2ME sample program shows how to make a SOCKET Connection from a J2ME Phone.

Many a times there is a need to connect a backend HTTP server from the J2ME application.


/*

 * SOCKET CONNECTION from J2ME phone to a HTTP Server
 *
 * @author pradeep Sundriyal
 *
 */

import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import java.io.*;

public class socket extends MIDlet {
  // StreamConnection allows bidirectional communication
  private StreamConnection streamConnection = null;
 
  // use OutputStream to send requests
  private OutputStream outputStream = null;
  private DataOutputStream dataOutputStream = null;
 
  // use InputStream to receive responses from Web server
  private InputStream inputStream = null;
  private DataInputStream dataInputStream = null;
 
  // specify the connect string
  private String connectString = "socket://url"; /*url is a valid web address  e.g  www.abc.com:80*/
 
  // use a StrignBuffer to store the retrieved page contents
  private StringBuffer results;
 
  // define GUI components
  private Display myDisplay = null;
  private Form resultScreen;
  private StringItem resultField;
 
  public socket() {
    // initializing GUI display
    results = new StringBuffer();
    myDisplay = Display.getDisplay(this);
    resultScreen = new Form("Page Content:");
  }
 
  public void startApp() {
    try {
      // establish a socket connection with remote server
      streamConnection =
      (StreamConnection) Connector.open(connectString);
      
      // create DataOuputStream on top of the socket connection
      outputStream = streamConnection.openOutputStream();
      dataOutputStream = new DataOutputStream(outputStream);
      
      // send the HTTP request
      dataOutputStream.writeChars("GET /index.htm HTTP/1.0 n");
      dataOutputStream.flush();
      
      // create DataInputStream on top of the socket connection
      inputStream = streamConnection.openInputStream();
      dataInputStream = new DataInputStream(inputStream);
      
      // retrieve the contents of the requested page from Web server
      int inputChar;
      while ( (inputChar = dataInputStream.read()) != -1) {
        results.append((char) inputChar);
      }
      
      // display the page contents on the phone screen
      resultField = new StringItem(null, results.toString());
      resultScreen.append(resultField);
      myDisplay.setCurrent(resultScreen);
      
    } catch (IOException e) {
      System.err.println("Exception caught:" + e);
    } finally {
      // free up I/O streams and close the socket connection
      try {
        if (dataInputStream != null)
          dataInputStream.close();
      } catch (Exception ignored) {}
      try {
        if (dataOutputStream != null)
          dataOutputStream.close();
      } catch (Exception ignored) {}
      try {
        if (outputStream != null)
          outputStream.close();
      } catch (Exception ignored) {}
      try {
        if (inputStream != null)
          inputStream.close();
      } catch (Exception ignored) {}
      try {
        if (streamConnection != null)
          streamConnection.close();
      } catch (Exception ignored) {}
    }
  }
 
  public void pauseApp() {
  }
 
  public void destroyApp(boolean unconditional) {
  }
}/* end of class



Blogger Image
My Blog Title

Product Engineering, software engineering company, Product Development, Product Migration, Product Re-engineering, Product Maintenance, Product Testing Commercial Application Development, Business Software development, commercial software for startups, Application Support and Maintenance, software testing Product Maintenance, Outsource product maintenance, product support and maintenance Product Migration, Product Re-engineering, product re-engineering services Product Research, Product Engineering, UI Prototyping Services Software Testing Services, Quality Assurance services, professional software testers, Load Testing, Functional Testing, Cross Platform, Browser Testing, Test Automation, Testing Tools, software quality analysis Functional Testing Services, software quality analysis, Software Testing Services, Application Testing Services, Functional Testing Types Automated Testing, Automated Testing Services, automation testing, test script development, Automation Test Tools, outsource automation testing Load Testing, Performance Testing Services, Load Testing Tools Offshore Software Development, Outsource software services, offshore outsourcing services, offshore software development services, IT outsourcing services, software quality assurance services, Offshore IT services, Custom Application Development Services, Offshore Product Engineering Benefits of IT Outsourcing, Offshore Software Development companies, offshore software development firms Outsource planning, IT outsourcing, IT development services, offshore IT companies, offshore software development Offshore Software Development, Outsource software services, offshore outsourcing services, offshore software development services, IT outsourcing services, software quality assurance services, Offshore IT services, Custom Application Development Services, Offshore Product Engineering Offshore Software Development, Outsource software services, offshore outsourcing services, offshore software development services, IT outsourcing services, software quality assurance services, Offshore IT services, Custom Application Development Services, Offshore Product Engineering