C#写的Socket Server端在unity运行时和关闭时没事,但是在打开直接unity崩溃问题

最近有个项目需要些Socket,正好之前也没有写过Socket就开始写Server端的。但是就出现了如标题所说的状况,上网搜了一下,知道了是在你Server关闭时,我没有关闭线程

所以unity会崩溃,然后按照下面的代码解决了这个问题;

using System.Net;  
using System.Net.Sockets;   
using System.Collections;   
using System.Text;  
using System.Threading;
public class SocketServer : MonoBehaviour {
       //这个是你开启的线程,结束时就得关闭
        public Thread ListenConnectThread;
    private TcpListener tcpListener = null;
        //这个函数是当应用程序退出时执行什么操作
    void OnApplicationQuit()
    {
                //关闭线程
        this.ListenConnectThread.Abort ();
//关闭监听
        tcpListener.Stop ();
    }

}

 

posted on 2016-01-29 09:27  萨瓦迪卡麦兜兜  阅读(2284)  评论(0编辑  收藏  举报

导航