Sample code which shows problem:
This implements simple http server which is called using devices browser.Code:import java.io.*; import javax.microedition.io.*; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Form; import javax.microedition.midlet.MIDlet; public class Midlet extends MIDlet { private Server s; private Display display; private Form form; private int PORT = 1025; public Midlet() { s = new Server(PORT, this); } public void startApp() { if (display == null) { display = Display.getDisplay(this); form = new Form("Socket test"); display.setCurrent(form); } s.startListen(); try { Thread.sleep(1000); } catch (InterruptedException e) {} try { // platformRequest("http://10.0.2.20:" + PORT); // Emulator ip platformRequest("http://localhost:" + PORT); // platformRequest("http://127.0.0.1:" + PORT); } catch (ConnectionNotFoundException e) { append(e.getMessage()); } } public void append(String s) { System.out.println(s); form.append(s + "\n"); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } } class Server { private ServerSocketConnection serverSocket; private int port = 0; private Thread serverThread = null; private Midlet midlet = null; private final static String REPLY = "HTTP/1.0 200 Ok\r\n" + "Content-Type: text/html; charset=\"utf-8\"r\n" + "\r\n" + "<html><body><p>It works!</p></body></html>\n"; public Server(int port, Midlet midlet) { this.midlet = midlet; this.port = port; } boolean startListen() { try { serverSocket = (ServerSocketConnection)Connector.open("socket://:" + port); midlet.append("Server running in: " + serverSocket.getLocalAddress() + ":" +serverSocket.getLocalPort()); serverThread = new Thread() { private StreamConnection connection = null; public void run() { midlet.append("Server thread started"); try { connection = serverSocket.acceptAndOpen(); midlet.append("Client connected to server"); InputStream is = connection.openInputStream(); InputStreamReader isr = new InputStreamReader(is); String input = new String(); int read = 0; while(isr.ready()) { read = isr.read(); if (read != -1) { input += (char)read; } else { break; } } isr.close(); is.close(); midlet.append(input); OutputStream os = connection.openOutputStream(); os.write(REPLY.getBytes("utf-8")); os.flush(); os.close(); } catch (IOException e) { midlet.append(e.getMessage()); } finally { try { if (connection != null) { connection.close(); connection = null; } if (serverSocket != null) { serverSocket.close(); serverSocket = null; } serverThread = null; } catch (IOException e) { midlet.append(e.getMessage()); } } } }; serverThread.start(); return true; } catch (IOException e) { midlet.append(e.getMessage()); return false; } finally { } } }
http://localhost or http://127.0.0.1 does not work in S40 emulator nor in device ( tried with few asha devices ).
Using HttpConnection in emulator connection is rejected and in device it timeouts. Using devices or emulators real IP connection works.
I also tested this sample in S60 and it worked as it was supposed to.
Question is, is connections to localhost somehow restricted or unsupported?

Reply With Quote

