Tuesday, September 09, 2003

J2ME: Turning your phone into web Server.

I wrote a simple midlet which turns my Phone into simple web server. To keep the code simple, I removed the additional GPS capabilities. Currently the web server listens to port 8000 and repsonds "Hello, I am Kumar Mettu's Phone. Thanks for Visiting" for all requests that are sent to this tiny web server. Note that this code is tested only on Motorola i88s with Nextel Total Connect Service.

Here is the code for the tiny web server:




001 import java.io.IOException;
002 import java.io.InputStream;
003 import java.io.PrintStream;
004 
005 import javax.microedition.io.Connector;
006 import javax.microedition.io.StreamConnection;
007 import javax.microedition.io.StreamConnectionNotifier;
008 import javax.microedition.lcdui.Display;
009 import javax.microedition.midlet.MIDlet;
010 
011 /**
012  * MIDlet working as simple personal WebServer.
013  * Currently this serves simple HTTP GET operation.
014  *
015  @author Kumar Mettu
016  @version 0.61
017  */
018 
019 public class WebServer extends MIDlet {
020 
021     private Display display;
022 
023     StreamConnectionNotifier scn = null;
024 
025     /**
026      * Default constructor.
027      */
028     public WebServer() {
029        display = Display.getDisplay(this);
030     }
031 
032     /**
033      * This will be invoked when we start the MIDlet
034      */
035     public void startApp() {
036         try {
037             scn =(StreamConnectionNotifier)Connector.open("serversocket://:8000");
038             while (true) {
039                 StreamConnection sc = (StreamConnection)scn.acceptAndOpen();
040 
041                 // service the connection in a separate thread
042                 Connection c = new Connection(sc);
043                 c.start();
044             }
045 
046 
047         catch (IOException e) {
048             //Handle Exceptions any other way you like.
049             //No-op
050         }
051     }
052 
053     /**
054      * Pause, discontinue ....
055      */
056     public void pauseApp() {
057         try {
058             if (scn != null)
059                 scn.close();
060         catch(Exception e) {
061         }
062 
063     }
064 
065     /**
066      * Destroy. Cleanup everything.
067      */
068     public void destroyApp(boolean unconditional) {
069         try {
070             if (scn != null)
071                 scn.close();
072         catch(Exception e) {
073         }
074     }
075 
076     /**
077      * Thread to handle client request.
078      */
079     class Connection extends Thread
080     {
081 
082         public Connection(StreamConnection c) {
083             client = c;
084         }
085 
086         /**
087          * Handles client request.
088          */
089         public void run() {
090             InputStream s = null;
091             PrintStream out = null;
092             StringBuffer b = new StringBuffer();
093             try {
094 
095                 s = client.openInputStream();
096 
097                  //Ignore reading request to reduce the amount of data
098                  //transfered to Phone.
099                 /*int ch;
100                 while((ch = s.read()) != -1) {
101                     b.append((char) ch);
102                 }
103                 System.out.println(b.toString());*/
104 
105                 out = new PrintStream(client.openOutputStream());
106                 String response =
107                              "<HTML>"+
108                              "<HEAD>"+
109                                 "<TITLE>Kumar's Location</TITLE>"+
110                              "</HEAD>"+
111                              "<BODY>Hello, I am Kumar Mettu's Phone. "+
112                                      "Thanks for Visiting.</BODY>"+
113                              "</HTML>";
114                 out.println("HTTP/1.0 200 OK\n");
115                 out.println(response);
116 
117             }
118             catch (Throwable ioe) {
119                 //Handle Exceptions any other way you like.
120                 //No-op
121             }
122             finally {
123                 try {
124                     if (s != null)
125                         s.close();
126                     if (out != null)
127                         out.close();
128                     if (client != null)
129                         client.close();
130                 }
131                 catch (IOException ioee) {
132           //Handle Exceptions any other way you like.
133           //No-op
134                 }
135             }
136         }
137 
138         private StreamConnection client;
139     }
140 
141 }
142   



I know there is no business value for this web server. But hey turning phone into a web server and viewing it serve web pages to browser is fun :-)

8 comments:

Anonymous said...

hey! i am no good at java. so can you pls explain what should i do to try the code an motorola slvr L2? and again how to i access the phone outside itself? say, the web or wap?

Anonymous said...

That produces
java.lang.ClassNotFoundException: com/sun/midp/io/j2me/serversocket/Protocol
at javax.microedition.io.Connector.openPrim(+277)
at javax.microedition.io.Connector.open(+15)
at javax.microedition.io.Connector.open(+6)
at javax.microedition.io.Connector.open(+5)
at Funktionen.WebServer.startApp(+6)
at javax.microedition.midlet.MIDletProxy.startApp(+7)
at com.sun.midp.midlet.Scheduler.schedule(+270)
at com.sun.midp.main.Main.runLocalClass(+28)
at com.sun.midp.main.Main.main(+80)
java.lang.ClassNotFoundException: com/sun/midp/io/j2me/serversocket/Protocol
at javax.microedition.io.Connector.openPrim(+277)
at javax.microedition.io.Connector.open(+40)
at javax.microedition.io.Connector.open(+6)
at javax.microedition.io.Connector.open(+5)
at Funktionen.WebServer.startApp(+6)
at javax.microedition.midlet.MIDletProxy.startApp(+7)
at com.sun.midp.midlet.Scheduler.schedule(+270)
at com.sun.midp.main.Main.runLocalClass(+28)
at com.sun.midp.main.Main.main(+80)

What is going on?

Kumar said...

Andreas,

Thats an indication MIDP Implementation on your phone does not support server sockets. The example was for old Nextel phones who had support for Server Sockets.

Kumar.

Anonymous said...

Hey, it's a very good idea to do that. And has the potential for commercial value. Imagine the possibilities of remote surveillance, social networking, etc. Sockets are base to many things.

Alex said...

Kumar, very interesting issue, but how do we access the web server page from a browser anywhere or by another mobile phone?

Unknown said...

Might be interesting for some offline functionality which synchronizes data with the back office if the phone is online again.

a_paddy said...

Opera are thinking of doing something similar with Opera Unite.

Unknown said...

I had to modify the serversocket to socket, but other than that, it's working fine. The only question that remains, is how do i connect to this server on the sam phone with a browser or web based widget?