📄 server.java
字号:
//Server.java 2004/04/01 tanaimport javax.microedition.midlet.*;import javax.microedition.io.*;import javax.microedition.lcdui.*;import javax.wireless.messaging.*;import java.io.*;public class Server implements CommandListener, Runnable { private SMSSocketTest parent; private Display display; private Form f; int NUM = 3; TextField[] tf; private boolean stop; Command sendCommand = new Command("Send", Command.ITEM, 1); // sms receive Command testCommand = new Command("RecvSMS", Command.OK, 1); //parameters used in socket connection StringItem[] strItem; String[] port = { "12100", "12101", "12102" }; InputStream[] is; OutputStream[] os; SocketConnection[] sc; Sender[] sender; ServerThread[] sth; // sms receive SmsRecv smsReceiver; public Server(SMSSocketTest m) { parent = m; display = Display.getDisplay(parent); f = new Form("Socket Server"); strItem = new StringItem[ NUM ]; is = new InputStream[ NUM ]; os = new OutputStream[ NUM ]; sc = new SocketConnection[ NUM ]; sender = new Sender[ NUM ]; sth = new ServerThread[ NUM ]; tf = new TextField[ NUM ]; for( int i=0; i<NUM; i++ ) { String tmp = "Port" + i + ":"; strItem[i] = new StringItem( tmp, " " ); tf[i] = new TextField( "Send:", "", 30, TextField.ANY ); f.append( strItem[i] ); f.append( tf[i] ); } // sms receive f.addCommand(testCommand); f.addCommand(sendCommand); f.setCommandListener(this); // sms receive smsReceiver = new SmsRecv( f, display ); } public void start() { display.setCurrent(f); for( int i=0; i<NUM; i++ ) { sth[i] = new ServerThread( i ); sth[i].start(); } } public void commandAction(Command c, Displayable s) { if (c == sendCommand && !parent.isPaused()) { for( int i=0; i<NUM; i++ ) { sender[i].send( tf[i].getString() ); } } if (c == Alert.DISMISS_COMMAND ) { parent.notifyDestroyed(); parent.destroyApp(true); } // sms receive if ( c==testCommand ) { Thread th = new Thread( this ); th.start(); } } // sms receive public void run() { smsReceiver.start(); } public void stop() { try { stop = true; //why here has no sender.stop()? for( int i=0; i<NUM; i++ ) { if (is[i] != null) { is[i].close(); } if (os[i] != null) { os[i].close(); } if (sc[i] != null) { sc[i].close(); } } } catch (IOException ioe) { } } class ServerThread extends Thread { int index; public ServerThread( int num ) { index = num; } public void run() { try { strItem[index].setText("Waiting for connection"); String urlString = "socket://:" + port[index]; ServerSocketConnection scn = (ServerSocketConnection) Connector.open(urlString); // Wait for a connection. sc[index] = (SocketConnection) scn.acceptAndOpen(); strItem[index].setText("Connection accepted"); is[index] = sc[index].openInputStream(); os[index] = sc[index].openOutputStream(); sender[index] = new Sender(os[index]); // Allow sending of messages only after Sender is created while (true) { StringBuffer sb = new StringBuffer(); int c = 0; while (((c = is[index].read()) != '\n') && (c != -1)) { sb.append((char) c); } strItem[index].setText("Message received - " + sb.toString()); } } catch (IOException ioe) { if (ioe.getMessage().equals("ServerSocket Open")) { Alert a = new Alert("Server", "Port 16100,16101,16102 is already taken.", null, AlertType.ERROR); a.setTimeout(Alert.FOREVER); // a.setCommandListener(this); display.setCurrent(a); } else { if (!stop) { ioe.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -