分享
 
 
 

通过J2ME程序做一个用户数据报协议连接

王朝java/jsp·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

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.knowsky.com)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有