📄 client.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */import java.io.IOException;import javax.microedition.io.Connector;import javax.microedition.io.Datagram;import javax.microedition.io.UDPDatagramConnection;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;/** * @author Administrator */public class Client extends MIDlet implements CommandListener{ private Display ds; private Form fm=new Form("Client"); private TextField tf= new TextField("输入内容","",255,TextField.ANY); private Command cmdSend= new Command("Send",Command.SCREEN,1); private UDPDatagramConnection udc; private Datagram dg; private final int MAX =255 ;//定义一个空间给接受的数据报最大字节数 public void startApp() { ds= Display.getDisplay(this); ds.setCurrent(fm); fm.append(tf); fm.addCommand(cmdSend); fm.setCommandListener(this); try { udc = (UDPDatagramConnection) Connector.open("datagram://192.168.0.101:9999"); } catch (IOException ex) { ex.printStackTrace(); } } public void commandAction(Command c,Displayable d){ if(c==cmdSend){ String str=tf.getString(); //发送数据报 try { dg = udc.newDatagram(str.getBytes(), str.getBytes().length); new ReceiveThread().start(); udc.send(dg);//送给服务器端 } catch (IOException ex) { ex.printStackTrace(); } } } class ReceiveThread extends Thread{ public void run(){ while(true){ //接受数据报 try{ dg= udc.newDatagram(MAX); udc.receive(dg); byte[] data=dg.getData(); fm.append(new String(data,0,dg.getLength())); }catch(Exception e){ e.printStackTrace(); } } } } public void pauseApp() { } public void destroyApp(boolean unconditional) { }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -