📄 server.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 Server extends MIDlet implements CommandListener{ private Display ds; private Form fm=new Form("Server"); private TextField tf= new TextField("输入内容","",255,TextField.ANY); private Command cmdSend= new Command("Send",Command.SCREEN,1); private Datagram dg; private UDPDatagramConnection udc; private final int MAX =255 ;//定义一个空间给接受的数据报最大字节数 private String adress;//保存客户端地址 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://:9999"); new ReceiveThread().start(); } 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); dg.setAddress(adress); 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); adress=dg.getAddress(); byte[] data=dg.getData(); String str=new String(data,0,dg.getLength()); fm.append(str+'\n'); }catch(Exception e){ e.printStackTrace(); } } } } public void pauseApp() { } public void destroyApp(boolean unconditional) { }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -