国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

通過J2ME程序做一個用戶數據報協議連接

2019-11-18 16:17:56
字體:
來源:轉載
供稿:網友

Make a UDP connection from a J2ME PRogram

This J2ME sample program shows how to make an UDP connection from a J2ME phone.

UDP connections as compared to HTTP connections are asynchronous and therefore lightweight. Since the consumer needs to pay for the GPRS usage charges based on the amount of data transferred from and to the phones, it is best to use UDP for many consumer based applications that are not too mUCh critical in nature. The tradeoff however is the lack of acknowledgement in UDP protocol. This can be overcome by implementing application level acknowledgement if necessary.

This free J2ME sample program illustrates how to make an UDP connection back to a server from the J2ME phone and send data back to the server. The same program can be changed to transfer data between two phones if they have static ip such as in Nextel's phones.

/* *  * This is a free J2ME sample program  * to make UDP CONNECTION from J2ME phone to a Server * * @author William Alexander * free for use as long as this comment is  * included in the program as it is *  * More Free java programs available for download  * at http://www.java-samples.com * */ import javax.microedition.lcdui.*; import javax.microedition.midlet.*; import javax.microedition.io.*; import java.util.*;
public class UDPTest extends MIDlet implements CommandListener {    static final String hostname = "203.123.12.201";    static final int serverport 	= 9000;	private Form mainScreen = null;    static final int sendport 		= 9000;    static final int receiveport 	= 9001;    private Form resultScreen = null;    static boolean isOver = false;	TextField message=null;     private Command okCommand = new Command("OK", Command.OK, 1);    private Command backCommand = new Command("Back", Command.BACK, 2);    private Command exitCommand = new Command("Exit", Command.EXIT, 2);    private Display myDisplay;    public UDPTest() {           myDisplay = Display.getDisplay(this);	resultScreen = new Form("Result");        resultScreen.addCommand(okCommand);        resultScreen.setCommandListener(this);	mainScreen = new Form("UDP Testing");	message=new TextField("Message:"," ",90,TextField.ANY);      //  mainScreen = new List("UDP Test", List.IMPLICIT);		}         public void startApp()            throws MIDletStateChangeException {	mainScreen.append(message);        mainScreen.addCommand(okCommand);        mainScreen.addCommand(exitCommand);        mainScreen.setCommandListener(this);        myDisplay.setCurrent(mainScreen);    }    public void pauseApp() {    }    public void destroyApp(boolean unconditional) {
} public void commandAction(Command c, Displayable d) { String res = null; if ((c == okCommand) && (d == mainScreen)) { // int selected = mainScreen.getSelectedIndex(); try { //switch (selected) { // case 0: res = sendRequest(message.getString()); resultScreen.append (res); myDisplay.setCurrent(resultScreen); // break; // } } catch (Exception e) { System.err.println(e); } } if ((c == exitCommand) && (d == mainScreen)) { notifyDestroyed(); } if ((c == okCommand) && (d == resultScreen)) { myDisplay.setCurrent(mainScreen); } } String sendRequest(String request) { DatagramListener dgl = new DatagramListener("datagram://:" + receiveport); dgl.start(); doSend("datagram://" + hostname + ":" + serverport, request); int counter = 0; outloop:while (!isOver) { if (counter++ > 5) break outloop; try { Thread.sleep(1000); } catch (Exception e) { System.out.println("wake up Neo!"); } } isOver = false; return dgl.data; return "Sent"; } void doSend(String destAddr, String msg) { int length = msg.length(); byte[] message = new byte[length]; DatagramConnection dc = null; System.arraycopy(msg.getBytes(), 0, message, 0, length); String localhost = "datagram://localhost:" + sendport; try { dc = (DatagramConnection)Connector.open(localhost); Datagram dobject = dc.newDatagram(message, length, destAddr); dc.send(dobject); } catch (Exception e) { System.out.println("Failed to send message: " + e); } finally { if (dc != null) { try { dc.close(); } catch (Exception f) { System.out.println("Failed to close Connector: " + f); } } } } class DatagramListener extends Thread { DatagramConnection dc = null; Datagram dobject; String data = "error"; DatagramListener(String recvAddr) { try { dc = (DatagramConnection)Connector.open(recvAddr); dobject = dc.newDatagram(dc.getMaximumLength()); } catch (Exception e) { System.out.println("Failed to initialize Connector"); } } public void run() { try { dc.receive(dobject); data = new String(dobject.getData(), 0, dobject.getLength());
isOver = true; } catch (Exception e) { System.err.println("Failed to receive message:" + e); } finally { if (dc != null) { try { dc.close(); } catch (Exception f) { System.err.println("Failed to close Connector: " + f); } } } } }}

(出處:http://m.survivalescaperooms.com)



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平罗县| 同德县| 习水县| 内黄县| 雷州市| 鄂伦春自治旗| 阳西县| 林周县| 威宁| 曲周县| 莎车县| 霍城县| 易门县| 江阴市| 鲁山县| 抚宁县| 邻水| 郑州市| 景谷| 大理市| 垦利县| 蕲春县| 哈巴河县| 宿州市| 京山县| 平乐县| 满洲里市| 项城市| 扬州市| 蒙自县| 大英县| 全州县| 偏关县| 石河子市| 旺苍县| 图木舒克市| 周口市| 博兴县| 永州市| 南昌市| 宜昌市|