4

I started to work on unity3d. I just wanna send/receive data between Unity3D and Raspberry Pi. I used socket programming for that. But when I try to connect my Raspberry Pi, Unity send me error message that "Unable to connect as no valid cross-domain policy was found". Why cross-domain policy is required. I just wanna send or receive a data between my pc and raspberry. Can I disable cross-domain policy request or how can I solve this problem.

public static void StartClient() {
    byte[] bytes = new byte[1024];
    int remotePort = xxxx; // My raspberry port
    try {
        IPHostEntry ipHostInfo = Dns.GetHostEntry("xxx.xxx.xxx.xxx"); //-->> my raspberry IP
        IPAddress ipAddress = ipHostInfo.AddressList[0];
        IPEndPoint remoteEP = new IPEndPoint(ipAddress,remotePort);

        Socket mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );

        try {
            mySocket.Connect(remoteEP);

            print("Socket connected to" + mySocket.RemoteEndPoint.ToString());

            byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>");

            int bytesSent = mySocket.Send(msg);

            int bytesRec = mySocket.Receive(bytes);
            print("Echoed test = " + Encoding.ASCII.GetString(bytes,0,bytesRec));

            mySocket.Shutdown(SocketShutdown.Both);
            mySocket.Close();

            }
        catch (ArgumentNullException ane) {
            print("ArgumentNullException : " + ane.ToString());
        } 
        catch (SocketException se) {
            print("SocketException : " + se.ToString());
        }
        catch (Exception e) {
            print("Unexpected exception : " + e.ToString());
        }

    }
    catch (Exception e) {
        Console.WriteLine( e.ToString());
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397

1 Answers1

1

You can't disable this. Is a seccurity protectión on WWW request and Sockets. You need to make on your pc an authorization for this connections. The deafult port for this seccurity cheching is 843.

You can use the included example program from Unity existing on the installation folder/Data/Tools/SocketPolicyServer or can make your own service running listening to this port or including an xml file if you have your Pc how a webserver something like this:

<?xml version="1.0"?>
<cross-domain-policy>
   <allow-access-from domain="*" to-ports="1200-1220"/> 
</cross-domain-policy>"
joreldraw
  • 1,736
  • 1
  • 14
  • 28
  • Thanks for your help but how can I add .xml file in my c# code – Burak Helvacı Jun 23 '15 at 00:20
  • You need to place your crossdomain.xml in the root you need to acces. If you are only using TCP connection with socket how i see in your code need the example program running listening to port 843. – joreldraw Jun 23 '15 at 07:06
  • If I need to place crossdomain.xml in my root, am I should place it in Windows\System32\drivers\etc folder. If that is one of the solution of my problem, I tried this way but it didn't work. I placed crossdomain.xml in Windows\System32\drivers\etc folder and I copied this: " in crossdomain.xml. But it didn't work – Burak Helvacı Jun 23 '15 at 08:48
  • crossdomain.xml is for www request root. If your for example need to get www request to www.myhost.com/data/myfile.x you need crossdomain on www.myhost.com/data. – joreldraw Jun 24 '15 at 07:01