⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datagramserver.java

📁 登陆用的源程序~~~不怎么样``但也是我初学的参考的有用的资料
💻 JAVA
字号:
package datagram;import javax.microedition.midlet.*;import javax.microedition.io.*;import javax.microedition.lcdui.*;import java.io.*;public class DatagramServer extends MIDlet implements Runnable, CommandListener {    private Display display;    private Command exitCommand = new Command("退出", Command.EXIT, 1);    private Command sendCommand = new Command("发送", Command.ITEM, 1);    private Form f;    private StringItem si;		// 用于要输入发送的字符串    private TextField tf;		// 用于显示接收到的字符串    Sender sender;				// 用于发送数据的线程    private String address;		// 保存客户端的地址	private UDPDatagramConnection dc;	// UDP数据报连接对象	public DatagramServer() {        display = Display.getDisplay(this);        f = new Form("Datagram Server");        si = new StringItem("状态:" , " ");        tf = new TextField("发送:", "", 30, TextField.ANY);        f.append(tf);        f.append(si);        f.addCommand(exitCommand);        f.addCommand(sendCommand);        f.setCommandListener(this);        display.setCurrent(f);    }    public void startApp() {        new Thread(this).start();	// 开始服务器端连接的线程    }    public void run() {        try {            si.setText("等待客户端连接……");	// 设置当前显示的状态			// 在端口5555以服务器模式打开数据报连接            dc = (UDPDatagramConnection)Connector.open("datagram://:5555");            sender = new Sender(dc);			// 建立线程,用于发送数据            while (true) {                Datagram dg = dc.newDatagram(100);  // 建立数据报用于接收数据                dc.receive(dg);						// 接收客户端发送的数据报                address = dg.getAddress();			// 从数据报中取得发送方的地址                si.setText("接收到消息 - " + new String(					dg.getData(), 0, dg.getLength()));		// 显示接收到的数据            }        } catch (Exception e) {			System.out.println("错误:"+e.getMessage());        }    }    public void pauseApp() { }    public void destroyApp(boolean unconditional) { }    public void commandAction(Command c, Displayable s) {        if (c == exitCommand) {            destroyApp(true);            notifyDestroyed();        } else if (c == sendCommand) {            if (address == null) {					// 地址为空,无法发送数据                si.setText("没有目的地址");            } else {                sender.send(address, tf.getString());	// 将输入的数据回复给发送方            }        }    }    // 关闭数据报连接    public void stop() {        try {            if (sender != null) sender.stop();            if (dc != null) dc.close();        } catch (IOException ioe) { }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -