Unity3D中的Socket通信

第一部分简单示例:连接服务器Ip 端口,发送请求,接收数据同理。

    public static void SocketConnect(string serverIP, int serverPort)  
        {  
            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  
            IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(serverIP), serverPort);  
            try  
            {  
                clientSocket.Connect(ipe);  
                Debug.Log(" Connect Success IP: " + serverIP + " Port : " + serverPort.ToString());  
            }  
            catch (Exception e)  
            {  
                Debug.LogError(e.ToString());  
            }  
        }  //Unity3D教程手册:www.unitymanual.com
        //发送数据  
        public static void Send(byte[] bytes)  
        {  
            if (clientSocket == null)  
                return;  
            if (!clientSocket.Connected)  
                return;  
            if (clientSocket.Poll(0, SelectMode.SelectWrite))  
            {  
                try  
                {  
                    clientSocket.Send(bytes);  
                }  
                catch (Exception e)  
                {  
                    Debug.LogError(e.ToString());  
                }  
            }  
        }  

第二部分请求数据示例:

  public void _MSG_ACCOUNT(string name, string pwd)  
       {  
           ACCOUNT account = new ACCOUNT();  
           datas = new byte[account.length];  
           byte[] bytesLength = BitConverter.GetBytes((System.Int16)account.length);  
           int length = 0;  
           bytesLength.CopyTo(datas, length);  
           length += 2;  
           byte[] bytesID = BitConverter.GetBytes((System.Int16)account.ID);  
           bytesID.CopyTo(datas, length);  
           length += 2;  
           byte[] bytesName = Encoding.Default.GetBytes(name);  
           bytesName.CopyTo(datas, length);  

第三部分接收数据示例:

    void _MSG_CONNECT(byte[] datas)  
        {  
 
            connect = new CONNECT();  
            int length = 4;  
            connect.idAccount = BitConverter.ToUInt32(datas, length);  
            Debug.LogError("账号ID          " + connect.idAccount);  
            //Unity3D教程手册:www.unitymanual.com
            length += System.Runtime.InteropServices.Marshal.SizeOf(connect.idAccount);  
            connect.dwData = BitConverter.ToUInt32(datas, length);  
            Debug.LogError("认证ID            " + connect.dwData);  
 
            length += System.Runtime.InteropServices.Marshal.SizeOf(connect.dwData);  
            connect.nServerIndex = BitConverter.ToUInt32(datas, length);  
            Debug.LogError("游戏逻辑服务器索引           " + connect.nServerIndex);  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小姑娘很大

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值