The security policy settings of xmlsocket communication are described in details in as3.

Source: Internet
Author: User

Flash security policies are involved in Flash communities or games with socket servers.
If the security policy is not verified, the socket connection fails.
There are several solutions to this problem, depending on the specific situation. One is to load a flash with the same location as flash, and the flash is connected to the server.
In addition, after connecting to the server, accept the policy file of the server and continue communication. For details, refer to the official flash. system. Security class.

Below I will list the solutions I have written. I directly write the configuration file into the server (here I will give an example. For simplicity, the server can actually read XML)

Now we can see all the server code:
/*
* $ ID: xmlserver. Java $
*
* @ Author soda. c sujun10@21cn.com
* @ Version 1.0
* <Br> copyright 2005-2006 the SDK-I Studio
* <Br> this program is protected by copyright laws.
* <Br> program name:
* <Br> date:
*/

Import java.net. Socket;
Import java.net. serversocket;
Import java. Io. printwriter;
Import java. Io. bufferedreader;
Import java. Io. inputstreamreader;

Public class xmlserver
{

Public void start () throws exception
{
String xml = "<cross-domain-Policy> ";
Xml = XML + "<allow-access-from domain =/" 127.0.0.1/"to-ports =/", 8080/"/> ";
Xml = XML + "<allow-access-from domain =/" localhost/"to-ports =/"/80/"/> ";
Xml = XML + "</Cross-Domain-Policy> ";

Serversocket = new serversocket (5000 );
While (true)
{
Try
{
// Create a connection
Socket socket = serversocket. Accept ();
System. Out. println ("connection successful ......");
Bufferedreader BR = new bufferedreader (New inputstreamreader (socket. getinputstream ()));
Printwriter PW = new printwriter (socket. getoutputstream ());
// Receiving Username
Char [] by = new char [22];
BR. Read (by, 0, 22 );
String head = new string ();
System. Out. println ("message header:" + head + ":");
If (head. Equals ("<policy-file-Request/> "))
{
PW. Print (XML + "/0 ");
PW. Flush ();
}
Else
{
Serverthread thread = new serverthread (socket );
Thread. Start ();
}
}
Catch (exception E)
{
System. Out. println ("the server encountered an exception! "+ E );
}
}
}
Public static void main (string [] ARGs)
{
Try
{
New xmlserver (). Start ();
}
Catch (exception E)
{
System. Out. println ("socket exception:" + E );
}

}
}

In the class,
Defines a character transfer in XML format
String xml = "<cross-domain-Policy> ";
Xml = XML + "<allow-access-from domain =/" 127.0.0.1/"to-ports =/", 8080/"/> ";
Xml = XML + "<allow-access-from domain =/" localhost/"to-ports =/"/80/"/> ";
Xml = XML + "</Cross-Domain-Policy> ";

// Receiving Username
Char [] by = new char [22];
BR. Read (by, 0, 22 );
String head = new string ();
System. Out. println ("message header:" + head + ":");
If (head. Equals ("<policy-file-Request/> "))
{
PW. Print (XML + "/0 ");
PW. Flush ();
}
Else
{
Serverthread thread = new serverthread (socket );
Thread. Start ();
}
This is based on the first string passed by the client, whether it is a login request or a security policy request.

Next, let's look at some client code.

Public Function testxmlsocket ()
{
System. usecodepage = true;
Socket = new xmlsocket ();
Socket. addeventlistener (event. Connect, onconnect );
Socket. addeventlistener (ioerrorevent. io_error, failconnect );
Socket. addeventlistener (dataevent. Data, ondatas );

Sendbtn. addeventlistener (mouseevent. mouse_down, sendevent );
Socket. Connect ("10.0.108.166", 5000 );
}

Public Function onconnect (mystatus: Event): void
{
Msgtext. Text = msgtext. Text + "connection successful/N ";
// Send the message to the server immediately
Socket. Send ("<policy-file-xxquest/>/N ");
}

As you can see, when the connection is successful, a judgment string is sent immediately.
This is because Flash player transfers <policy-file-Request/> immediately after a connection is established using the specified port and ends with NULL bytes.
The server sends an empty byte to terminate the policy file and then closes the connection. If the server does not close the connection, flash player will do the same after receiving the null byte.
After the client obtains the permission, it automatically connects to the server again and then responds to the onconnect method.

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.