?? udpmidlet.java
字號(hào):
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class UDPMIDLet extends MIDlet {
private static UDPMIDLet instance;
private UDPScreen displayable = new UDPScreen(this);
public UDPMIDLet() {
// TODO Auto-generated constructor stub
instance = this;
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
Display.getDisplay(this).setCurrent(displayable);
}
}
class UDPScreen extends Form implements CommandListener
{
private UDPMIDLet udpmidlet;
public UDPScreen(UDPMIDLet udp)
{
super("UDP顯示");
this.udpmidlet = udp;
try
{
setCommandListener(this);
addCommand(new Command("連接",Command.OK,1));
}
catch(Exception e)
{e.printStackTrace();}
}
class UDPConnector implements java.lang.Runnable
{
public void run() {
connect();
}
}
public void connect()
{
Datagram revdata,senddata;
byte[] s=null;
try
{
UDPDatagramConnection con = (UDPDatagramConnection)
Connector.open("datagram://192.168.0.168:5555");
String clientmsg = "Hello Server!";
senddata = con.newDatagram(clientmsg.getBytes(), clientmsg.length());
con.send(senddata);
revdata=con.newDatagram(con.getMaximumLength());
con.receive(revdata);
con.close();
s = revdata.getData();
}
catch(Exception e)
{e.printStackTrace();}
TextBox input = new TextBox("Server reply:","whatisthis",50000,TextField.ANY);
input.setString(new String(s));
Display.getDisplay(udpmidlet).setCurrent(input);
}
public void commandAction(Command cmd, Displayable arg1) {
// TODO Auto-generated method stub
if(cmd.getCommandType() == Command.OK)
{
new java.lang.Thread(new UDPConnector()).start();
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -