flash socket 学习笔记 服务器为C++
- package
- {
- import flash.net.Socket;
- import flash.events.*;
- import flash.display.Sprite;
- import flash.errors.*;
- import flash.display.SimpleButton;
- public class demo extends Sprite
- {
- private var mysocket:Socket;
- private var host:String="127.0.0.1";
- private var port:int=5000;
- public function demo()
- {
- trace("构造函数!!!!");
- mysocket=new Socket();
- mysocket.addEventListener(Event.CONNECT,OnConnect); //--当连接成功时
- mysocket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); //连接失败时
- mysocket.addEventListener(ProgressEvent.SOCKET_DATA , receivedata); //返回服务器传输回来的数据
- //mysocket.addEventListener(SecurityError
- mysocket.connect(host,port);
- //发送数据
- mysocket.writeUTFBytes("i am flash\n");
- mysocket.flush();//发送数据
- }
- private function OnConnect(e:Event):void
- {
- trace("连接成功");
- mysocket.writeUTFBytes("Test successful2\n");
- mysocket.flush();//发送数据
- }
- private function ioErrorHandler(e:IOErrorEvent):void
- {
- trace("连接失败");
- }
- private function receivedata(e:ProgressEvent):void
- {
- trace("收到的字节数"+mysocket.bytesAvailable);
- var msg:String;
- while (mysocket.bytesAvailable)
- {
- msg+=mysocket.readMultiByte(mysocket.bytesAvailable,"utf8");
- trace(msg);
- }
- }
- private function SendData(e:MouseEvent):void
- {
- trace("发送");
- mysocket.writeUTFBytes("i am flash\n");
- mysocket.flush();//发送数据
- }
- }
- }
C++服务器,发送什么返回什么。
-
Server.rar (4.75 KB)