J2ME聊天应用程序无法连接

时间:2015-06-11 17:26:38

标签: java java-me midlet

我根据这里的代码创建了一个聊天应用程序: https://anujarosha.wordpress.com/2011/07/26/chat-application-in-j2me/

使用IP地址从客户端连接到服务器。但是,当在两个不同的手机上进行测试时,客户端没有连接到服务器。这是应用程序的代码:

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;
}
}

这是客户:

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);
        }
}
}

请帮我解决这个错误,以便我可以通过两部手机和远距离连接它。

1 个答案:

答案 0 :(得分:1)

连接到互联网的手机通常不会获得公共IP。分配给它们的IP在本地网络(WiFi,3G等)内有效,当它们需要连接到网络外的东西时,路由将通过代理执行。

您最好使用公共固定IP创建服务器,并让移动客户端连接到该服务器。想想亚马逊,Heroku,Azure等服务