Blackberry フォーラムには、4.5.0.x から 4.5.0.83 までのデータグラムの問題に関するコメントがあります。SDK に UDP サンプルがないのも不思議ではありません。http://na.blackberry.com/eng/developers/からいつでも 8100 と 4.5.0.108 シミュレーターをダウンロードできます。
ホスト名はAPI参照で許可されていますが、MDSシミュレーターを使用すると、 localhost alias を取得します。
次のコードでは、ポート 135 をリッスンする単純なサーバーと、ポート 135 の 127.0.0.1 にデータ パケットを送信する bb クライアントがあります。
デスクトップ サーバー コード:
public static void main(String[] args) {
byte[] inBuff = new byte[32];
DatagramSocket socket;
try {
socket = new DatagramSocket(137);
DatagramPacket pckt = new DatagramPacket(inBuff, inBuff.length);
while (true) {
socket.receive(pckt);
System.out.println(new Date() + " " + pckt.getAddress()
+ ":" + pckt.getPort());
socket.send(pckt);
}
} catch (Exception e) {
System.out.println(e.getMessage()+":");
System.out.println(e.getClass().getName());
}
}
BlackBerry クライアント コード (4.6.1 で Bold 8900 でテスト済み):
UDPDatagramConnection connection = null;
byte[] outBuff = "Hello!".getBytes();
Datagram outDatagram = null;
try {
connection = (UDPDatagramConnection) Connector
.open("datagram://127.0.0.1:137");
outDatagram = connection.newDatagram(outBuff, outBuff.length);
connection.send(outDatagram);
System.out.println("Datagram packet was sent");
} catch (Exception e) {
System.out.println(e.getMessage()+":");
System.out.println(e.getClass().getName());
}