package SMSServer; import javax.microedition.midlet.*; import javax.microedition.io.*; import javax.microedition.lcdui.*; import javax.wireless.messaging.*; import java.io.IOException; public class SMSServer extends MIDlet implements CommandListener, MessageListener { private Command mExitCommand = new Command("Exit", Command.EXIT, 0); private Display mDisplay = null; private Form mForm = null; public void commandAction(Command c, Displayable d) { destroyApp(true); notifyDestroyed(); } public void startApp() { mForm = new Form("Chapter 11 Server"); mForm.addCommand(mExitCommand); mForm.setCommandListener(this); Display.getDisplay(this).setCurrent(mForm); try { mForm.append("Registering"); MessageConnection conn = (MessageConnection)Connector.open("sms://:6001"); conn.setMessageListener(this); } catch (IOException ioExc) { mForm.append("Server connection could be not obtained"); System.out.println("Server connection could be not obtained"); ioExc.printStackTrace(); } } public void notifyIncomingMessage(MessageConnection conn) { Message msg = null; try { mForm.append("Incoming\n"); msg = conn.receive(); if (msg instanceof TextMessage) { String msgReceived = ((TextMessage)msg).getPayloadText(); mForm.append ("Received:"+msgReceived); System.out.println("Received:" + msgReceived); } } catch (IOException e) { mForm.append("Error:" + e); System.out.println("Error:" + e); } } public void pauseApp() { } public void destroyApp(boolean unconditional) { } }