赞
踩
官方的代码案例 ,注释行是可发送中文字符的方式
- 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")); //解析中文字符
- }
- }}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。