J2ME chat application not connecting

62 views Asked by At

I created a chat application based on the code here: https://anujarosha.wordpress.com/2011/07/26/chat-application-in-j2me/

which use IP adress to connect from the client to the server. But the client is not connecting to the server, when testing on two different phones. This is the code for the application:

public void startApp() {
        display = Display.getDisplay(this);
        chatServerForm = new Form("Chat Server");
        startServerStrItem = new StringItem("Start Chat Server \t", "Start", Item.BUTTON);
        chatServerForm.append(startServerStrItem);
        startCmd = new Command("Start", Command.ITEM, 8);
        startServerStrItem.setDefaultCommand(startCmd);
        startServerStrItem.setItemCommandListener(this);
        spacerItem = new Spacer(200, 50);
        chatServerForm.append(spacerItem);
        gaugeItem = new Gauge("Waiting for client... \n", false, Gauge.INDEFINITE, Gauge.CONTINUOUS_RUNNING);
        gaugeItem.setLayout(Item.LAYOUT_CENTER);
        chatServerForm.append(gaugeItem);
        exitCmd = new Command("Exit", Command.EXIT, 7);
        chatServerForm.addCommand(exitCmd);
        chatServerForm.setCommandListener(this);
        display.setCurrent(chatServerForm);
        showIpForm = new Form("Chat Server");
        ipStrItem = new StringItem("Client must connect to this IP: \n\n", "My IP \n");
        ipStrItem.setLayout(Item.LAYOUT_CENTER);
        ipStrItem.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE));
        showIpForm.append(ipStrItem);
        showIpForm.addCommand(exitCmd);
        showIpForm.setCommandListener(this);
        srvChattingFrm = new Form("Server Chatting...");
        chatTxtFld = new TextField("Enter Chat", "", 160, TextField.ANY);
        srvChattingFrm.append(chatTxtFld);
        sendCmd = new Command("Send", Command.OK, 4);
        srvChattingFrm.addCommand(sendCmd);
        srvChattingFrm.addCommand(exitCmd);
        srvChattingFrm.setCommandListener(this);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void commandAction(Command c, Displayable d) {
        if (c == exitCmd) {
                notifyDestroyed();
        } else if (c == sendCmd) {
                srvChatMsg = chatTxtFld.getString();
                msgSenderClass.send(srvChatMsg);
        }
}
public void commandAction(Command c, Item item) {
        if (c == startCmd) {
                new Thread(this).start();
                Alert alert = new Alert("Working");
                display.setCurrent(alert);
        }
}
public void run() {
        System.out.println("Running");
        try {
                serSocCon = (ServerSocketConnection) Connector.open("socket://:60000");
                System.out.println("Open the socket...");
        } catch (IOException ex) {
                ex.printStackTrace();
        }
        try {
                deviceIp = serSocCon.getLocalAddress();
                System.out.println("Get device IP...");
        } catch (IOException ex) {
                ex.printStackTrace();
        }
        showIpForm.append(deviceIp);
        display.setCurrent(showIpForm);
        try {
                System.out.println("Waiting for client request...");
                socCon = (SocketConnection) serSocCon.acceptAndOpen();
                System.out.println("Accepted and open the connection...");
        } catch (IOException ex) {
                ex.printStackTrace();
        }
        try {
                is = socCon.openDataInputStream();
        } catch (IOException ex) {
                ex.printStackTrace();
        }
        try {
                os = socCon.openDataOutputStream();
        } catch (IOException ex) {
                ex.printStackTrace();
        }
        msgSenderClass = new MessageSender(os, chatTxtFld);
        msgSenderClass.start();
        MessageReceiver msgReceiverClass = new MessageReceiver(is, srvChattingFrm);
        msgReceiverClass.start();
        infoAlert = new Alert("Client connected successfully.");
        display.setCurrent(infoAlert, srvChattingFrm);
}
public void clear() {
        chatTxtFld.setString("");
        msgSenderClass.start();
}
public TextField getChatTxtFld() {
        return chatTxtFld;
}
}

And this is the client:

public void startApp() {
        display = Display.getDisplay(this);
        sendIpForm = new Form("Chat Client");
        ipTxtFld = new TextField("Type server IP here \n", "", 24, TextField.ANY);
        sendIpForm.append(ipTxtFld);
        exitCmd = new Command("Exit", Command.EXIT, 7);
        sendIpForm.addCommand(exitCmd);
        connectCmd = new Command("Connect", Command.OK, 4);
        sendIpForm.addCommand(connectCmd);
        sendIpForm.setCommandListener(this);
        display.setCurrent(sendIpForm);
        chatForm = new Form("Client Chatiing...");
        chatMsgTxtFld = new TextField("Enter chat", "", 160, TextField.ANY);
        chatForm.append(chatMsgTxtFld);
        sendCmd = new Command("Send", Command.OK, 4);
        chatForm.addCommand(sendCmd);
        chatForm.addCommand(exitCmd);
        chatForm.setCommandListener(this);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void commandAction(Command c, Displayable d) {
        if (c == exitCmd) {
                notifyDestroyed();
        } else if (c == connectCmd) {
                new Thread(this).start();
        } else if (c == sendCmd) {
                chatMsg = chatMsgTxtFld.getString();
                msgSenClass.send(chatMsg);
        }
}
public void run() {
        srvIp = ipTxtFld.getString();
        if (srvIp != null && srvIp.length() > 0) {
                try {
                        socCon = (SocketConnection) Connector.open("socket://" + srvIp + ":60000");
                        is = socCon.openDataInputStream();
                        os = socCon.openDataOutputStream();
                } catch (IOException ex) {
                        ex.printStackTrace();
                }
                msgSenClass = new MessageSender(os, chatMsgTxtFld);
                msgSenClass.start();
                MessageReceiver msgRecClass = new MessageReceiver(is, chatForm);
                msgRecClass.start();
                infoAlert = new Alert("Server connected successfully.");
                display.setCurrent(infoAlert, chatForm);
        }
}
}

Please help me to solve this Error, so that I can connect it through two phones and in distance.

1

There are 1 answers

0
Telmo Pimentel Mota On

Phones connected to the internet usually does not get public IPs. The IP assigned to them is valid inside a local network (WiFi, 3G, etc) and when they need to connect to something out of the network the routing will be performed with a proxy.

You better create a server with a public fixed IP and have the mobile clients connect to it. Think of services like Amazon, Heroku, Azure, etc.