AS3 UDP 套接字 (AIR)

官方的代码案例 ,注释行是可发送中文字符的方式

package 
{ 
    import flash.display.Sprite; 
    import flash.events.DatagramSocketDataEvent; 
    import flash.events.Event; 
    import flash.net.DatagramSocket; 
    import flash.utils.ByteArray; 

    public class DatagramSocketExample extends Sprite 
    { 
        private var datagramSocket:DatagramSocket; 

    //The IP and port for this computer 本机
    private var localIP:String = "127.0.0.1"; 
    private var localPort:int = 10101;

    //The IP and port for the target computer 目标计算机
    private var targetIP:String = "127.0.0.1"; 
    private var targetPort:int = 10000;

    public function DatagramSocketExample() 
    { 
        //Create the socket 
        datagramSocket = new DatagramSocket(); 
        datagramSocket.addEventListener( DatagramSocketDataEvent.DATA, dataReceived ); 

        //将套接字绑定到本地网络接口和端口(端口,IP)
        datagramSocket.bind( localPort  ); 

        //Listen for incoming datagrams 
        datagramSocket.receive(); 

        //Create a message in a ByteArray
        var data:ByteArray = new ByteArray();
        data.writeUTFBytes("Hello.");
        // data.writeMultiByte("你好,中文字符GB2312汉字","gb2312"); //发送中文字符

        //向目标计算机发送消息
        datagramSocket.send( data, 0, 0, targetIP, targetPort); 
    } 

    private function dataReceived( event:DatagramSocketDataEvent ):void 
    { 
        //Read the data from the datagram 
        trace("Received from " + event.srcAddress + ":" + event.srcPort + "> " + 
            event.data.readUTFBytes( event.data.bytesAvailable ) );
        // trace(event.data.readMultiByte( event.data.bytesAvailable, "gb2312")); //解析中文字符
    } 
}}

参考来源:https://help.adobe.com/zh_CN/as3/dev/WSb2ba3b1aad8a27b0-181c51321220efd9d1c-8000.html#WSb2ba3b1aad8a27b0-181c51321220efd9d1c-7ffe

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值