Hi guys, just trying to use sockets in my app and unity crashes when declaring a new socket:
Just this line crashes unity entirely, even when put on a coroutine
Socket soc = listener.AcceptSocket();
What I’m missing?
Here is full server code:
using UnityEngine;
using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Net.Sockets;
public class ReceiveStuff : MonoBehaviour {
TcpListener listener;
// Use this for initialization
void Start ()
{
listener = new TcpListener(2333);
listener.Start();
Debug.Log("Server Iniciado");
StartCoroutine("listen");
}
IEnumerator listen()
{
while(true)
{
Socket soc = listener.AcceptSocket();
Debug.Log("Conectados" + soc.RemoteEndPoint.ToString());
try
{
Stream s = new NetworkStream(soc);
StreamReader sr = new StreamReader(s);
StreamWriter sw = new StreamWriter(s);
sw.AutoFlush = true;
while(true)
{
string name = sr.ReadLine();
if(name == "" || name == null)
{
}
else
{
Debug.Log("RECEBIDO: " + name);
}
}
s.Close();
}
catch(Exception e)
{
Debug.Log("EXCEPTION: " + e.Message);
}
soc.Close();
yield return null;
}
}