×

Discussion Board

Results 1 to 2 of 2

Hybrid View

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    1

    localhost problems in S40

    Sample code which shows problem:
    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 {
            }
        }
    }
    This implements simple http server which is called using devices browser.

    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?

  2. #2
    Registered User
    Join Date
    Aug 2011
    Posts
    26

    Re: localhost problems in S40

    Hello, for me address 10.0.2.15 works on the S40 emulator that comes with the Nokia SDK 2.0.
    I adjusted the address in your code and it worked.
    In case you are wondering where I got this IP from: In your code you call
    Code:
    midlet.append("Server running in: " + serverSocket.getLocalAddress() + ":" +serverSocket.getLocalPort());
    This displayed 10.0.2.15 (on S40 and 127.0.0.1 on S60).

    To get the hostname if you don't have access to the serverSocket you can use:
    Code:
    System.getProperty("microedition.hostname")
    Like this in your case:
    Code:
            try {
                platformRequest("http://" + System.getProperty("microedition.hostname") + ":" + PORT);
            } catch (ConnectionNotFoundException e) {
                append(e.getMessage());
            }

    I hope this helped.


    Regards,
    UnPlan2ned

Similar Threads

  1. communicating on the localhost
    By vinp in forum Symbian Networking & Messaging (Closed)
    Replies: 1
    Last Post: 2010-11-12, 10:28
  2. localhost
    By benkar in forum Symbian Tools & SDKs
    Replies: 1
    Last Post: 2010-04-06, 10:21
  3. localhost
    By benkar in forum Symbian Tools & SDKs
    Replies: 0
    Last Post: 2010-04-06, 08:38
  4. localhost goes through IAP?
    By davidbr4 in forum Symbian
    Replies: 0
    Last Post: 2008-06-06, 15:27
  5. Connecting to localhost
    By kevinhankinson in forum Bluetooth Technology
    Replies: 0
    Last Post: 2004-09-21, 18:08

Posting Permissions

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