国产探花免费观看_亚洲丰满少妇自慰呻吟_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://www.survivalescaperooms.com)



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临安市| 麻江县| 石柱| 漯河市| 邮箱| 黄陵县| 榆社县| 新津县| 永清县| 黎城县| 望城县| 息烽县| 尼木县| 三穗县| 朝阳县| 紫云| 朝阳区| 恩平市| 化德县| 夏河县| 余江县| 南通市| 中江县| 石渠县| 女性| 嵊泗县| 嘉鱼县| 北流市| 迁西县| 颍上县| 霞浦县| 靖江市| 察隅县| 黑水县| 浦东新区| 连江县| 光泽县| 绵竹市| 赤壁市| 宜城市| 麟游县|