源自:http://www.builder.com.cn/2007/1228/692442.shtml)
在J2ME中实现基于UDP协议通讯程序
开发者在线 Builder.com.cn 更新时间:2007-12-28作者:mingjava 来源:J2ME开发网
本文关键词: J2ME 实现 UDP协议 通讯程序
同样,我们要获得DatagramConnection的话,必须通过Connector的open方法,其中的URL应该满足如下的形式。
- datagram://localhost:5555 这样的话表示建立了一个客户端模式的连接。在指定ip:localhost和指定端口:5555
- datagram://:5555 这样建立的是一个服务器端模式的连接,在本地的5555端口。
建立连接后,我们可以通过DatagramConnection的newDatagram()方法构造一个Datagram,然后调用DatagramConnection的send()方法。这样数据报将会发送到指定的接受方。例如你可以构建这个一个负责发送数据的Sender类。
package com.siemens.datagramtest; import javax.microedition.io.Datagram; public class Sender extends Thread private DatagramConnection dc; private String address; private String message; public Sender(DatagramConnection dc) public synchronized void send(String addr, String msg) public synchronized void run() while (true) // If no client to deal, wait until one connects try // Completed client handling, return handler to pool and } |
注意联网的时候我们应该在另外一个线程中而不是在主线程中。
服务器端的目的就是启动后监听指定的端口,当客户端连接过来后接受数据并记录下客户端的地址,以便服务器端向客户端发送数据。
package com.siemens.datagramtest; import java.io.IOException; import javax.microedition.io.Connector; public class Server implements Runnable, CommandListener private DatagramMIDlet parent; private Display display; private Form f; private StringItem si; private TextField tf; private Command sendCommand = new Command("Send", Command.ITEM, 1); Sender sender; private String address; public Server(DatagramMIDlet m) public void start() Thread t = new Thread(this); public void run() si.setText("Waiting for connection"); sender = new Sender(dc); while (true) } catch (IOException ioe) public void commandAction(Command c, Displayable s) public void stop() } |
客户端代码则是建立连接后向服务器端发送数据,并等待接受服务器返回的数据。
package com.siemens.datagramtest; import java.io.IOException; import javax.microedition.io.ConnectionNotFoundException; public class Client implements Runnable, CommandListener private DatagramMIDlet parent; private Display display; private Form f; private StringItem si; private TextField tf; private Command sendCommand = new Command("Send", Command.ITEM, 1); Sender sender; public Client(DatagramMIDlet m) } public void start() public void run() DatagramConnection dc = (DatagramConnection) Connector si.setText("Connected to server"); sender = new Sender(dc); while (true) } catch (ConnectionNotFoundException cnfe) public void commandAction(Command c, Displayable s) public void stop() } |
本文的代码取自WTK demo中的例子,您可以参考demo中的源代码!下面给出MIDlet的代码
package com.siemens.datagramtest; import javax.microedition.lcdui.Choice; public class DatagramMIDlet extends MIDlet implements CommandListener private static final String SERVER = "Server"; private static final String CLIENT = "Client"; private static final String[] names = { SERVER, CLIENT }; private static Display display; private Form f; ChoiceGroup cg; private boolean isPaused; private Command exitCommand = new Command("Exit", Command.EXIT, 1); private Command startCommand = new Command("Start", Command.ITEM, 1); public DatagramMIDlet() f.addCommand(exitCommand); display.setCurrent(f); public static Display getDisplay() public boolean isPaused() public void startApp() public void pauseApp() public void destroyApp(boolean unconditional) public void commandAction(Command c, Displayable s) } |