在gcf中提供了datagramconnection和datagram两个接口,借助他们我们可以在j2me中基于udp协议开发联网应用程序。在midp2.0中,添加了udpdatagramconnection这个接口,他扩展了datagramconnection并添加了两个方法getlocaladdress()和getlocalport()。我们知道udp服务是不可靠的,如果你希望开发更可靠的联网应用的话可以采用socketconnection,因为tcp服务是面向连接且可靠的。我们还必须清楚地一点是以上所说的各种连接方式都不是midp规范中规定必须实现的。因此在使用之前请参考特定设备的开发文档。midp中只有http连接是必须支持的。
同样,我们要获得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) } |
闽公网安备 35060202000074号