查看: 3021|回复: 1
打印 上一主题 下一主题

[经验分享] unity3D Socket连接C# 服务器出现怪了

[复制链接]

1557

主题

1

听众

1万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
454
精华
31

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2013-10-30 16:52:39 |只看该作者 |倒序浏览
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();


        }


    }
}
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

0

主题

1

听众

2286

积分

中级设计师

Rank: 5Rank: 5

纳金币
0
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2013-10-31 18:01:29 |只看该作者
search the answer from unity official forum
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-16 11:32 , Processed in 0.088942 second(s), 31 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部