ActionScript 3.0 uses Xmlsocket and server communication programs (source code) _flash AS3

Source: Internet
Author: User
Tags socket error strcmp
Copy Code code as follows:

//
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 + + based server Source:
Copy Code code as follows:

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"));
}
}
Note The server-side program is an asynchronous socket class that inherits CAsyncSocket from MFC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.