MisaelGaray
MisaelGaray

Reputation: 75

Sockets with unity and Web Player

I'm working with .Net sockets in untity and it works fine when I export the standalone application for any desktop OS, even when I run the application directly from unity projects, but the problem starts when I use the unity web player. It refused the conexion because the ports, I guess. I changed the port to what i'm using for the application an now i didn't refuse the client connection but it still does not works, aparently the data from the client does not reach the server and nothing is happening. Does the Unity web player works fine with sockets?

public class SocketMain : MonoBehaviour {
System.Threading.Thread SocketThread;
volatile bool keepReading = false;

string alto = "", ancho ="";
int al = 0, an = 0;
// Use this for initialization
void Start () {
    Application.runInBackground = true;
    startServer();
}
bool bandera = false;
// Update is called once per frame
void Update () {

    if(al != 0 && an != 0)
    {
        Grid grid = gameObject.AddComponent<Grid>();
        grid.xSize = al;
        grid.zSize = an;
        al = 0;
        an = 0;
    }

}



void startServer (){
    SocketThread = new System.Threading.Thread(networkCode);
    SocketThread.IsBackground = true;
    SocketThread.Start();
}

private string getIPAddress()
{
    IPHostEntry host;
    string localIP = "";
    host = Dns.GetHostEntry(Dns.GetHostName());
    foreach (IPAddress ip in host.AddressList)
    {
        if (ip.AddressFamily == AddressFamily.InterNetwork)
        {
            localIP = ip.ToString();
        }

    }
    return localIP;
}

Socket listener;
Socket handler;

void networkCode()
{
    string data;

    // Data buffer for incoming data.
    byte[] bytes = new Byte[1024];

    // host running the application.
    Debug.Log("Ip " + getIPAddress().ToString());
    IPAddress[] ipArray = Dns.GetHostAddresses("127.0.0.1");
    IPEndPoint localEndPoint = new IPEndPoint(ipArray[0], 1755);

    // Create a TCP/IP socket.
    listener = new Socket(ipArray[0].AddressFamily,
        SocketType.Stream, ProtocolType.Tcp);

    // Bind the socket to the local endpoint and 
    // listen for incoming connections.

    try
    {
        listener.Bind(localEndPoint);
        listener.Listen(10);

        // Start listening for connections.
        while (true)
        {
            keepReading = true;

            // Mientras se espera la conexion de un cliente.
            Debug.Log("Esperando Conexxion");     

            handler = listener.Accept();
            Debug.Log("Se ha Desconectado el cluiente");     
            data = null;


            while (keepReading)
            {
                bytes = new byte[1024];

                int bytesRec = handler.Receive(bytes);

                Debug.Log("Received from Server");

                if (bytesRec <= 0)
                {
                    keepReading = false;
                    handler.Disconnect(true);
                    break;
                }
                //Datos recibidos y concatenados
                data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
                //Pintamos data complto
                Debug.Log(data);

                var definition = new {alto = "", ancho ="" };

                //Deserealizamos data
                var medidas = JsonConvert.DeserializeAnonymousType(data, definition);

                Debug.Log("alto :" + medidas.alto + " , ancho :" + medidas.ancho);

                //Termina deserializacion
                alto = medidas.alto;
                ancho = medidas.ancho;
                al = int.Parse(alto);
                an = int.Parse(ancho);





                if (data.IndexOf("<EOF>") > -1)
                {
                    break;
                }

                System.Threading.Thread.Sleep(1);
            }

            System.Threading.Thread.Sleep(1);
        }
    }
    catch (Exception e)
    {
        Debug.Log(e.ToString());
    }
}

void stopServer()
{
    keepReading = false;

    //stop thread
    if (SocketThread != null)
    {
        SocketThread.Abort();
    }

    if (handler != null && handler.Connected)
    {
        handler.Disconnect(false);
        Debug.Log("Disconnected!");
    }
}

void OnDisable()
{
    stopServer();
}

}

This is my server class

Upvotes: 0

Views: 648

Answers (1)

Jerry Switalski
Jerry Switalski

Reputation: 2720

It is not that .NET Sockets don't work in Unity3D WebPlayer, but multithreading doesn't work.

Multithreading is not available in webplayers, but not due to Unity, from what I recall its a browser plugin security part, that plugins can not run multiple threads / spawn new threads

http://forum.unity3d.com/threads/webplayer-multi-threading.76087/

Upvotes: 1

Related Questions