当前位置:   article > 正文

AS3 UDP 套接字 (AIR)

as3 udp

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

  1. package
  2. {
  3. import flash.display.Sprite;
  4. import flash.events.DatagramSocketDataEvent;
  5. import flash.events.Event;
  6. import flash.net.DatagramSocket;
  7. import flash.utils.ByteArray;
  8. public class DatagramSocketExample extends Sprite
  9. {
  10. private var datagramSocket:DatagramSocket;
  11. //The IP and port for this computer 本机
  12. private var localIP:String = "127.0.0.1";
  13. private var localPort:int = 10101;
  14. //The IP and port for the target computer 目标计算机
  15. private var targetIP:String = "127.0.0.1";
  16. private var targetPort:int = 10000;
  17. public function DatagramSocketExample()
  18. {
  19. //Create the socket
  20. datagramSocket = new DatagramSocket();
  21. datagramSocket.addEventListener( DatagramSocketDataEvent.DATA, dataReceived );
  22. //将套接字绑定到本地网络接口和端口(端口,IP)
  23. datagramSocket.bind( localPort );
  24. //Listen for incoming datagrams
  25. datagramSocket.receive();
  26. //Create a message in a ByteArray
  27. var data:ByteArray = new ByteArray();
  28. data.writeUTFBytes("Hello.");
  29. // data.writeMultiByte("你好,中文字符GB2312汉字","gb2312"); //发送中文字符
  30. //向目标计算机发送消息
  31. datagramSocket.send( data, 0, 0, targetIP, targetPort);
  32. }
  33. private function dataReceived( event:DatagramSocketDataEvent ):void
  34. {
  35. //Read the data from the datagram
  36. trace("Received from " + event.srcAddress + ":" + event.srcPort + "> " +
  37. event.data.readUTFBytes( event.data.bytesAvailable ) );
  38. // trace(event.data.readMultiByte( event.data.bytesAvailable, "gb2312")); //解析中文字符
  39. }
  40. }}

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

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号