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

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

Datagram數據報的接收和發送

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

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.            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://m.survivalescaperooms.com)



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 涞源县| 西宁市| 隆子县| 龙州县| 于田县| 泾川县| 湾仔区| 海伦市| 昌平区| 望都县| 大同县| 望江县| 贵州省| 宜兴市| 镇巴县| 莱西市| 仁布县| 留坝县| 罗甸县| 乐亭县| 泽库县| 滦平县| 琼中| 曲周县| 六盘水市| 兴隆县| 长岛县| 柳州市| 藁城市| 咸丰县| 象山县| 登封市| 昆山市| 庆城县| 延长县| 龙南县| 腾冲县| 会理县| 呼伦贝尔市| 恩平市| 固阳县|