ActionScript 3.0中用XMLSocket与服务器通讯程序(源码)

所属分类: 网页制作 / Flash 阅读数: 1308
收藏 0 赞 0 分享
复制代码 代码如下:

//
// CXMLSocket.as
//
//
// Written by Leezhm, 20th Oct, 2008
// Contact : Leezhm@luxoom.cn
//
package
{
import flash.events.DataEvent;
import flash.events.Event;
import flash.events.IEventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.net.XMLSocket;
import flash.system.Security;
public class CXMLSocket extends XMLSocket
{
// declare variables
private var mHostName:String = "127.0.0.1";
private var mPort:int = 7654;
private var mStrRecvBuf:String;
// constructor
public function CXMLSocket():void
{
//ConfigNetEvent(this);
Connect();
}
public function Connect():void
{
var xmlStr:String = "xmlsocket://";
xmlStr += mHostName;
xmlStr += ":";
xmlStr += mPort;
Security.loadPolicyFile(xmlStr);
trace(xmlStr);
ConfigNetEvent(this);
this.connect(mHostName, mPort);
}
private function ConfigNetEvent(dispatcher:IEventDispatcher):void
{
dispatcher.addEventListener(Event.CONNECT, OnConnect);
dispatcher.addEventListener(Event.CLOSE, OnClose);
dispatcher.addEventListener(DataEvent.DATA, OnSocketData);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, OnIOError);
dispatcher.addEventListener(ProgressEvent.PROGRESS, OnProgress);
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, OnSecurityError);
}
private function OnConnect(ArgEvent:Event):void
{
trace("OnConnect--->" + ArgEvent);
this.send("Connected");
}
private function OnClose(ArgEvent:Event):void
{
trace("OnClose--->" + ArgEvent);
}
private function OnSocketData(ArgEvent:DataEvent):void
{
trace(ArgEvent.text);
}
private function OnIOError(ArgEvent:IOErrorEvent):void
{
trace("OnIOError--->" + ArgEvent.text);
}
private function OnProgress(ArgEvent:ProgressEvent):void
{
trace("OnProgress--->" + ArgEvent.bytesLoaded +
" Total:" + ArgEvent.bytesTotal);
}
private function OnSecurityError(ArgEvent:SecurityErrorEvent):void
{
trace("OnSecurityError--->" + ArgEvent);
}
}
}

基于C++的服务器源码:
复制代码 代码如下:

// Server.cpp : implementation file
//
#include "stdafx.h"
#include "Server.h"
#include "SerialPort.h"
extern CSerialPort m_Ports;
// CServer
extern CPtrArray gSocketArr;
extern bool gbIsConnected;
CServer::CServer():mStrSendBuf("")
{
//bIsConnectFlash = false;
}
CServer::~CServer()
{
}
// CServer member functions
void CServer::OnAccept(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
if(0 == nErrorCode)
{
CServer * pSocket = new CServer();
if(SOCKET_ERROR != this->Accept(*pSocket))
{
gSocketArr.Add(pSocket);
}
else
{
::AfxMessageBox(_T("Accept->Error"));
}
pSocket = NULL;
}
CAsyncSocket::OnAccept(nErrorCode);
}
void CServer::OnReceive(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
if(0 == nErrorCode)
{
char buf[30] = {0};
int nRead = this->Receive(buf, 30);
if(0 != nRead && SOCKET_ERROR != nRead)
{
//
// debug info
//
std::fstream debug;
debug.open(_T("Debug.txt"), std::ios::app);
int yvalue;
if(debug.is_open())
{
debug<<buf<<"\n";
yvalue = atoi(buf);
//m_Ports.WriteToPort(
}
debug.close();
if(0 == strcmp("<policy-file-request/>", buf))
{
std::string strXML = "<cross-domain-policy>";
strXML += "<allow-access-from domain=\"*\"to-ports=\"*\"/>";
//strXML += "<allow-access-from domain=\"localhost\"to-ports=\"1025,9999\"/>";
strXML += "</cross-domain-policy>";
strXML += "\0\0";
this->Send(strXML.c_str(), (int)strXML.length() + 1);
gbIsConnected = true;
}
else if(0 == strcmp("Connected", buf))
{
////::AfxMessageBox(_T("Hello"));
gbIsConnected = true;
}
}
}
CAsyncSocket::OnReceive(nErrorCode);
}
void CServer::SendData()
{
if(0 == gSocketArr.IsEmpty())
{
for(int i = 0; i < gSocketArr.GetSize(); i++)
{
((CServer *)gSocketArr[i])->Send(mStrSendBuf.c_str(), (int)mStrSendBuf.length() + 1);
}
std::fstream debug;
debug.open(_T("Debug.txt"), std::ios::app);
int yvalue;
if(debug.is_open())
{
debug<<mStrSendBuf<<"\n";
}
debug.close();
}
else
{
::AfxMessageBox(_T("Socket Error"));
}
}
注意服务器端程序是继承了MFC中CAsyncSocket的一个异步socket类
更多精彩内容其他人还在看

了解在Flash中的编程工作第1/4页

了解在Flash中的编程工作
收藏 0 赞 0 分享

FLV视频上传到网上不能播放的原因与解决办法

FLV视频上传到网上不能播放的原因与解决办法
收藏 0 赞 0 分享

Flash与后台数据交换方法整理

Flash与后台数据交换方法整理
收藏 0 赞 0 分享

FLASH基础开发习惯第1/2页

FLASH基础开发习惯
收藏 0 赞 0 分享

网页中嵌入Flash的方法讨论

网页中嵌入Flash的方法讨论
收藏 0 赞 0 分享

flash组件开发要点第1/2页

flash组件开发要点
收藏 0 赞 0 分享

修改flash之必备软件硕思闪客精灵3.3注册破解版下载

硕思闪客精灵 MX 是一款用于浏览和解析Flash动画(.swf文件和.exe文件)的工具
收藏 0 赞 0 分享

使用 AllowNetworking Flash的世界安静了

使用 AllowNetworking Flash的世界安静了
收藏 0 赞 0 分享

FCS中ActionScript代码提示功能

FCS中ActionScript代码提示功能
收藏 0 赞 0 分享

Flash ActionScript 中按钮和电影剪辑的事件和方法

Flash ActionScript 中按钮和电影剪辑的事件和方法
收藏 0 赞 0 分享
查看更多