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

📄 datagramclient.java

📁 登陆用的源程序~~~不怎么样``但也是我初学的参考的有用的资料
💻 JAVA
字号:
package datagram;import javax.microedition.midlet.*;import javax.microedition.io.*;import javax.microedition.lcdui.*;import java.io.*;public class DatagramClient 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 TextField tf;		// 用于输入要发送的数据    private StringItem si;		// 用于显示接收到的数据    Sender sender;				// 用于发送数据的线程	private UDPDatagramConnection dc;	// UDP数据报连接对象	public DatagramClient() {        display = Display.getDisplay(this);        f = new Form("Datagram Client");        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 {			// 客户端数据报连接,发送到主机localhost的5555端口            dc = (UDPDatagramConnection)Connector.open("datagram://localhost:5555");            si.setText("连接到服务器……");		// 设置当前显示的状态            sender = new Sender(dc);			// 建立线程,用于发送数据            while (true) {                Datagram dg = dc.newDatagram(100);		// 建立数据报用于接收数据                dc.receive(dg);							// 接收客户端发送的数据报                // 判断是否接收到了数据                if (dg.getLength() > 0) {                    si.setText("接收到消息 - " + new String(						dg.getData(), 0, dg.getLength()));	// 显示接收到的数据                }            }        } catch (IOException ioe) {			System.out.println("错误:"+ioe.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) {            // 地址为空,将输入的数据发送给打开连接的服务器			sender.send(null, 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 + -