纳金网

标题: unity3D Socket连接C# 服务器出现怪了 [打印本页]

作者: star-小虎    时间: 2013-10-30 16:52
标题: unity3D Socket连接C# 服务器出现怪了
unity3D Socket与C#服务器第一次连接时通讯正常,客服端段关闭后,unity3D编辑器再次启动连接 unity3D编辑器立即卡死,但是C#服务器与AS3Socket客服端多次连接就正常,大家帮帮,在下感激不敬。一直没找到unity3D 和C#服务器通讯的框架,大家有什么好推荐的
客服端 unity3D 代码:using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Text;
using System;
using System.IO;
using System.Runtime.Remoting.Messaging;

public class NewBehaviourScript : MonoBehaviour
{

    // Use this for initialization
    private Socket socket = null;

    private int i = 0;
    private NetworkStream networkStream;
    private TcpClient tcpClient;
    void Start()
    {

        initSocket();
    }

    // Update is called once per frame
    void Update()
    {

    }
    private void initSocket()
    {
        if (socket != null)
        {
            socket.Close();
            socket = null;
        }
        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        socket.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080));
        Thread thread = new Thread(new ThreadStart(addThread));
        thread.IsBackground = true;
        thread.Start();

    }
    private void addThread()
    {
        bool live = true;
        while (live)
        {
            try
            {
                byte[] bytes = new byte[byte.MaxValue];
                socket.Receive(bytes);
                Debug.Log(Encoding.UTF8.GetString(bytes));

            }
            catch (SocketException exe)
            {
                live = false;
                socket.Close();
                socket = null;
            }

        }


    }

    void getData(object s)
    {
        bool live = true;
        Socket so = (Socket)s;

        if (live)
        {
            try
            {
                byte[] bytes = new byte[byte.MaxValue];
                so.Receive(bytes);
                Debug.Log(Encoding.UTF8.GetString(bytes));
            }
            catch (Exception e)
            {
                live = false;
            }
        }
    }
    void OnGUI()
    {
        if (GUILayout.Button("send"))
        {
            i++;
            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes("data" + i);
                socket.Send(bytes);
            }
            catch (SocketException ex)
            {
                socket.Close();
                socket = null;
            }

        }

    }
}




         服务器C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Threading;
using System.Collections;
using System.Runtime.Remoting.Messaging;

namespace WebApplication5
{
    public partial class _Default : System.Web.UI.Page
    {

        private static int i = 0;
        private static Socket socket;
        private TcpListener tcpLister;
        protected void Page_Load(object sender, EventArgs e)
        {


            if (IsPostBack) return;

            begin();

        }

        void test()
        {
            bool live = true;
            while (live)
            {
                try
                {
                    if (tcpLister.Pending())
                    {

                        Socket socket = tcpLister.AcceptSocket();
                        Thread thread = new Thread(new ParameterizedThreadStart(getData));
                        thread.IsBackground = true;
                        thread.Start(socket);

                    }
                }
                catch (Exception exc)
                {
                    live = false;
                    tcpLister.Stop();

                }

            }
        }


        void getData(Object s)
        {
            Socket socket = (Socket)s;
            bool live = true;
            while (live)
            {
                try
                {
                    byte[] bytes = new byte[100];

                    socket.Receive(bytes);
                    i++;

                    socket.Send(bytes);

                }

                catch (Exception exce)
                {
                    live = false;
                    socket.Close();
                    socket = null;
                }
            }

        }

        void begin()
        {
            tcpLister = new TcpListener(IPAddress.Parse("127.0.0.1"), 8080);
            tcpLister.Start();
            Thread thread = new Thread(new ThreadStart(test));
            thread.IsBackground = true;
            thread.Start();


        }


    }
}

作者: orokborokhulu    时间: 2013-10-31 18:01
search the answer from unity official forum




欢迎光临 纳金网 (http://www.narkii.com/club/) Powered by Discuz! X2.5