WEB开发网      婵犵數濞€濞佳囧磹婵犳艾鐤炬い鎰堕檮閸嬬喐銇勯弽銊с€掗梻鍕閺岋箑螣娓氼垱笑闂佽姘﹂褔婀佸┑鐘诧工妤犲憡绂嶉崜褏纾奸弶鍫涘妼缁楁岸鏌熷畡鐗堝殗闁诡喒鏅犲畷褰掝敃閵堝棙顔忔繝鐢靛仦閸ㄥ爼骞愰幘顔肩;闁规崘绉ぐ鎺撳亹闁绘垶锕╁Λ鍕⒑閹肩偛濡奸悗娑掓櫇缁顓兼径妯绘櫇闂佹寧绻傞弻濠囨晝閸屾稓鍘甸柣搴㈢⊕閿氶柣蹇ョ稻缁绘繃绻濋崘銊т紝闂佽鍨伴崯鏉戠暦閻旂⒈鏁傞柛鈾€鏅欑槐妯衡攽閻愬樊鍤熷┑顔藉劤铻為柛鏇ㄥ墯閸欏繘鏌嶉崫鍕櫣缂佲偓婢跺绠鹃柟瀛樼箘閿涘秵顨ラ悙顏勭伈闁诡喖缍婂畷鎯邦槻婵℃彃顭烽弻娑㈠Ω閵夈儺鍔夌紓浣稿€哥粔褰掑极閹剧粯鏅搁柨鐕傛嫹 ---闂傚倷鐒︾€笛兠洪埡鍛闁跨噦鎷�
开发学院软件开发Java Datagram数据报的接收和发送 阅读

Datagram数据报的接收和发送

 2007-12-23 12:29:13 来源:WEB开发网 闂傚倷绶氬ḿ褍螞閹绢喖绠柨鐕傛嫹闂傚倷绀侀幉锟犲垂閻㈠灚宕查柟鎵閸庡秵銇勯幒鎴濃偓鐢稿磻閹炬枼妲堟繛鍡楃С濞岊亞绱撻崒姘扁枌闁瑰嚖鎷�婵犵數濮幏鍐川椤撴繄鎹曢梻渚€娼уú銈吤洪妸鈺佺劦妞ゆ帊鑳堕埊鏇㈡煏閸モ晛浠х紒杈╁仱閺佹捇鏁撻敓锟�闂傚倷绶氬ḿ褍螞閹绢喖绠柨鐕傛嫹  闂傚倷鑳舵灙缂佺粯顨呴埢宥夊即閵忕姵鐎梺缁樺姈椤愮厧鈽夊Ο閿嬬€婚梺褰掑亰閸撴稑鈻斿鑸碘拺闁告稑饪村▓鏃€绻涚仦鍌氬闁崇粯鎹囬獮瀣攽閹邦剚顔傛俊鐐€栧濠氬储瑜忛幉鎾晸閿燂拷
核心提示:Datagram Receiver import java.io.IOException;import javax.microedition.io.Connector;import javax.microedition.io.Datagram;import javax.microedition.io.DatagramC

Datagram Receiver

import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;
import javax.microedition.io.DatagramConnection;

public class DatagramReceiver {
  public static void main(String[] args) {
    if (args.length != 1) {
      System.out.PRintln("Usage: DatagramReceiver port");
      System.exit(1);
    }
    
    try {
      DatagramConnection receiver = 
          (DatagramConnection)Connector.open("datagram://:" + args[0]);
      byte[] buffer = new byte[256];
      Datagram dgram = receiver.newDatagram(buffer, buffer.length);
      for (;;) {
        dgram.setLength(buffer.length);
        receiver.receive(dgram);
        int length = dgram.getLength();
        System.out.println("Datagram received. Length is " + length);

        // Show the content of the datagram.
        for (int i = 0; i < length; i++) {
          System.out.print(buffer[i] + " ");
        }
        System.out.println();
        
        // Send it back...
        receiver.send(dgram);
      }
    } catch (IOException ex) {
      System.out.println("IOException: " + ex);
    }
  }
}
DatagramSender
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;
import javax.microedition.io.DatagramConnection;

public class DatagramSender {
  public static void main(String[] args) {
    if (args.length != 2) {
      System.out.println("Usage: DatagramSender port length");
      System.exit(1);
    }
    
    try {
      DatagramConnection sender = 
          (DatagramConnection)Connector.open("datagram://localhost:" + args[0]);
      int length = Integer.parseInt(args[1]);
      byte[] buffer = new byte[length];
      for (int i = 0; i < length; i++) {
        buffer[i] = (byte)('0' + (i % 10));
      }
      Datagram dgram = sender.newDatagram(buffer, buffer.length);
      sender.send(dgram);
      
      // Wait for the packet to be returned
      for (int i = 0; i < length; i++) {
        buffer[i] = (byte)0;
      }
      sender.receive(dgram);
      length = dgram.getLength();
      System.out.println("Received return packet, length is " + length);
      
      // Show the content of the datagram.
      for (int i = 0; i < length; i++) {
        System.out.print(buffer[i] + " ");
      }
      System.out.println();
    } catch (IOException ex) {
      System.out.println("IOException: " + ex);
    }
  }
}


(出处:http://www.cncms.com)


Tags:Datagram 数据 接收

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接