Socket: error

Hi all, I’m rookie, I’d like to ask for your help. Please.
I have the following code running on Windows in Unity Editor. (Reference links: http://kadri.synamicd.com/Unity/TCPConnection.cs)
using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Net.Sockets;
public class TCPConnection : MonoBehaviour {
//the name of the connection, not required but better for overview if you have more than 1 connections running
public string conName = “Localhost”;

//ip/address of the server, 127.0.0.1 is for your own computer
public string conHost = “192.168.0.177”;

//port for the server, make sure to unblock this in your router firewall if you want to allow external connections
public int conPort = 843;

//a true/false variable for connection status
public bool socketReady = false;
TcpClient mySocket;
NetworkStream theStream;
StreamWriter theWriter;
StreamReader theReader;
void Start(){
Security.PrefetchSocketPolicy (“localhost”, 843);
}
void Update(){
}
//try to initiate connection
public void setupSocket() {
try {
mySocket = new TcpClient(conHost, conPort);
theStream = mySocket.GetStream();
theWriter = new StreamWriter(theStream);
theReader = new StreamReader(theStream);
socketReady = true;
}
catch (Exception e) {
Debug.Log(“Socket error:” + e);
}
}

//send message to server
public void writeSocket(byte[ ] SendBuffer) {
if (!socketReady)
return;
theStream.Write(SendBuffer, 0, SendBuffer.Length);
theWriter.Flush();
}

//read message from server
public string readSocket() {
String result = “”;
if (theStream.DataAvailable) {
Byte[ ] inStream = new Byte[mySocket.SendBufferSize];
theStream.Read(inStream, 0, inStream.Length);
result = inStream[0].ToString();
}
return result;
}

//disconnect from the socket
public void closeSocket() {
if (!socketReady)
return;
theWriter.Close();
theReader.Close();
mySocket.Close();
socketReady = false;
}

//keep connection alive, reconnect if connection lost
public void maintainConnection(){
if(!theStream.CanRead) {
setupSocket();
}
}

}

When running the program, reported error. Here’s a message:
Socket error:System.Security.SecurityException: Unable to connect, as no valid crossdomain policy was found
at System.Net.Sockets.Socket.Connect_internal (IntPtr sock, System.Net.SocketAddress sa, System.Int32& error, Boolean requireSocketPolicyFile) [0x00000] in :0
at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP, Boolean requireSocketPolicy) [0x00000] in :0
at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in :0
at System.Net.Sockets.TcpClient.Connect (System.Net.IPEndPoint remote_end_point) [0x00000] in :0
at System.Net.Sockets.TcpClient.Connect (System.Net.IPAddress[ ] ipAddresses, Int32 port) [0x00000] in :0
UnityEngine.Debug:Log(Object)
TCPConnection:setupSocket() (at Assets/Scripts/TCPconnection.cs:38)
socketScript:OnGUI() (at Assets/Scripts/socketScript.cs:50)

[code ][/code ] tags please. I’ll wait until you edit your post and add them in.

Welcome to the wonderful world of sandbox security. Good luck!

@GroZZleR : Can you guide me? Please

That manual page is going to be way more thorough than I could be here on the forum.

Take your time and read the entire page from top to bottom. It goes over the creation of the crossdomain.xml file, where you have to put it and how to debug the issue if it persists.

Dude, srsly CODE tags!

Look how much easier this is too read!

using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Net.Sockets;

public class TCPConnection : MonoBehaviour {

    #region Fields
   
    [Tooltip("the name of the connection, not required but better for overview if you have more than 1 connections running")]
    public string conName = "Localhost";
    [Tooltip("ip/address of the server, 127.0.0.1 is for your own computer")]
    public string conHost = "192.168.0.177";
    [Tooltip("port for the server, make sure to unblock this in your router firewall if you want to allow external connections"]
    public int conPort = 843;
    [Tooltip("a true/false variable for connection status")]
    public bool socketReady = false;
   
   
    private TcpClient mySocket;
    private NetworkStream theStream;
    private StreamWriter theWriter;
    private StreamReader theReader;
   
    #endregion
   
    #region START
   
    void Start(){
        Security.PrefetchSocketPolicy ("localhost", 843);
    }
   
    #endregion
   
    #region Socket Methods
   
    //try to initiate connection
    public void setupSocket() {
        try {
            mySocket = new TcpClient(conHost, conPort);
            theStream = mySocket.GetStream();
            theWriter = new StreamWriter(theStream);
            theReader = new StreamReader(theStream);
            socketReady = true;
        }
        catch (Exception e) {
            Debug.Log("Socket error:" + e);
        }
    }

    //send message to server
    public void writeSocket(byte[] SendBuffer) {
        if (!socketReady) return;
        theStream.Write(SendBuffer, 0, SendBuffer.Length);
        theWriter.Flush();
    }

    //read message from server
    public string readSocket() {
        string result = string.Empty;
        if (theStream.DataAvailable) {
            byte[] inStream = new byte[mySocket.SendBufferSize];
            theStream.Read(inStream, 0, inStream.Length);
            result = inStream[0].ToString();
        }
        return result;
    }

    //disconnect from the socket
    public void closeSocket() {
        if (!socketReady) return;
        theWriter.Close();
        theReader.Close();
        mySocket.Close();
        socketReady = false;
    }

    //keep connection alive, reconnect if connection lost
    public void maintainConnection(){
        if(!theStream.CanRead) {
            setupSocket();
        }
    }

    #endregion

}

As for your error, the answer is IN the error:

You’re attempting to access a socket on a remote source (across domain, crossdomain), and that remote source does not have a crossdomain policy set up to allow you access.

@lordofduct : Set up? how? Can you guide me? please