📄 tesmo.java
字号:
/*
* @(#)SocketMIDlet.java 1.7 04/01/27
*
* Copyright (c) 2005 Kuka Microsystems, Inc. All rights reserved.
* PROPRIETARY/CONFIDENTIAL
* Use is subject to license terms
*/
package socket;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.wireless.messaging.*;
import java.io.*;
public class Tesmo extends MIDlet implements CommandListener {
public String clientNum;
public String serverIP;//="localhost";
public int incomingPortNum = 5000; // Port to listen for connection
private static Display display;
private Form f;
private ChoiceGroup cg;
private TextField cnum;
private StringItem messageString;
private boolean isPaused;
private Server server;
private Client client;
private boolean isUser;
private boolean srv;
private MessageConnection incomingConnection = null; // Connection for receiving the message
private Message incomingMessage; // Incoming message
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
private Command startCommand = new Command("Start", Command.ITEM, 1);
private Command sendCommand = new Command("Send", Command.ITEM, 1);
public Tesmo() {
// Was the MIDlet started by an incoming connection?
String connectList[];
connectList = PushRegistry.listConnections(true);
if (connectList == null || connectList.length == 0){
// Started by the user
isUser = true;
}else{
// Started by message
try
{
incomingConnection = (MessageConnection) Connector.open("sms://:" + incomingPortNum);
// Receive the message
incomingMessage = incomingConnection.receive();
}catch(IOException e){
System.out.println("IO Exception!"+e);
}
isUser = false;
}
// Start part of MIDlet depending on opener
if(isUser==true){
display = Display.getDisplay(this);
f = new Form("Tesmo");
cnum = new TextField("Enter client number:", null, 120, 0x0);
f.append(cnum);
f.addCommand(exitCommand);
f.addCommand(startCommand);
f.setCommandListener(this);
srv = true;
display.setCurrent(f);
}else if(isUser==false){
String inc = incomingMessage.getAddress();
serverIP = ((TextMessage)incomingMessage).getPayloadText();
inc = inc.substring(6);
display = Display.getDisplay(this);
f = new Form("Tesmo");
messageString = new StringItem("Incoming connection from "+inc+"!", "\nPress \"Start\" to connect.\nOr \"Exit\" to quit.");
f.append(messageString);
f.addCommand(exitCommand);
f.addCommand(startCommand);
f.setCommandListener(this);
srv = false;
display.setCurrent(f);
}
}
public boolean isPaused() {
return isPaused;
}
public void startApp() {
isPaused = false;
}
public void pauseApp() {
isPaused = true;
}
public void destroyApp(boolean unconditional) {
if (server != null) {
server.stop();
}
if (client != null) {
client.stop();
}
}
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(true);
notifyDestroyed();
} else if (c == startCommand) {
if (srv == true) {
clientNum = cnum.getString();
if (!isValidPhoneNumber(clientNum)) {
cnum.setString("Wrong number!");
}else{
server = new Server(this);
server.start();
}
} else {
client = new Client(this);
client.start();
}
}
}
private static boolean isValidPhoneNumber(String number) { char[] chars = number.toCharArray(); if (chars.length == 0) { return false; } int startPos = 0; // initial '+' is OK if (chars[0] == '+') { startPos = 1; } for (int i = startPos; i < chars.length; ++i) { if (!Character.isDigit(chars[i])) { return false; } } return true; }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -