zoukankan      html  css  js  c++  java
  • Unity Socket TCP

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using System.Net.Sockets;
    using System.Net;
    using System.Text;
    using System;
    using System.Threading;
    
    namespace HelloSocket
    {
        /// <summary>
        ///配置信息
        /// </summary>
        /// ip地址 端口 类型:TCP/UDP
        class HelloAddress
        {
            private static string host ="127.0.0.1";
            private static int port = 5055;
            private static IPAddress ip = IPAddress.Parse(host);
            public static IPEndPoint IPEP = new IPEndPoint(ip, port);
            public static Socket hSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            
            ///<summary>
            ///修改配置信息
            ///</summary>
            ///ip地址 端口 类型:TCP/UDP
            public static void SetHelloAddress(string ipStr, int iPort, System.Net.Sockets.ProtocolType type)
            {
                host = ipStr;
                port = iPort;
                ip = IPAddress.Parse(host);
                IPEP = new IPEndPoint(ip, port);
                hSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, type);
            }
        }
    
        ///<summary>
        ///客户端
        ///</summary>
        ///  API
        class HelloClient : HelloAddress
        {
            ///<summary>
            ///连接服务端
            ///</summary>
            public static void Main()
            {
                try
                {
                    hSocket.Connect(IPEP);
                    Thread threadConnect = new Thread(new ThreadStart(ReceiveMessage));
                    threadConnect.Start();
                }
                catch (ArgumentNullException e)
                {
                    Debug.Log(e.ToString());
                }
                catch (SocketException e)
                {
                    Debug.Log(e.ToString());
                }
            }
    
            ///<summary>
            ///发送信息
            ///</summary>
            public static void SendMessage(string sendStr)
            {
                //确定是否连接
                if (hSocket.Connected)
                {
                    IPEndPoint ipe = (IPEndPoint)hSocket.RemoteEndPoint;
                    sendStr = ipe.ToString() + sendStr;
                    byte[] bs = Encoding.ASCII.GetBytes(sendStr);
                    hSocket.Send(bs, bs.Length, 0);
                }
            }
    
            ///<summary>
            ///接收信息
            ///</summary>
            private static void ReceiveMessage()
            {
                while (true)
                {
                    byte[] recvBytes = new byte[1024];
                    int iBytes = hSocket.Receive(recvBytes, recvBytes.Length, 0);
                    if (iBytes <= 0)
                    {
                        break;
                    }
                    string recvStr = "";
                    recvStr+=Encoding.ASCII.GetString(recvBytes,0,iBytes);
                    Debug.Log("服务端返回的数据:" + recvStr);
                }
            }
            ///<summary>
            ///关闭套接字
            ///<summary>
            public static void Close()
            {
                hSocket.Close();
            }
        }
    
        ///<summary>
        ///服务端
        ///</summary>
        /// API
        class HelloServer : HelloAddress
        {
            //存储连接服务端的所有用户
            public static List<Socket> listPlayer = new List<Socket>();
            private static Socket sTemp;
            ///<summary>
            ///服务端 绑定地址并监听
            ///</summary>
            public static void Main()
            {
                try
                {
                    Thread threadListenAccept = new Thread(new ThreadStart(ListenAccept));
                    threadListenAccept.Start();
                }
                catch (ArgumentNullException e)
                {
                    Debug.Log(e.ToString());
                }
                catch (SocketException e)
                {
                    Debug.Log(e.ToString());
                }
            }
    
            ///<summary>
            ///监听用户连接
            ///</summary>
            private static void ListenAccept()
            {
                hSocket.Bind(IPEP);
                hSocket.Listen(0);
                sTemp = hSocket.Accept();
                listPlayer.Add(sTemp);
                Thread threadReceiveMessage = new Thread(new ThreadStart(ReceiveMessage));
                threadReceiveMessage.Start();
                while (true)
                {
                    sTemp = hSocket.Accept();
                    listPlayer.Add(sTemp);
                }
            }
            ///<summary>
            ///接收信息
            ///<summary>
            private static void ReceiveMessage()
            {
                while (true)
                {
                    byte[] recvBytes = new byte[1024];
                    int iBytes =sTemp.Receive(recvBytes, recvBytes.Length, 0);
                    if (iBytes <= 0)
                    {
                        break;
                    }
                    string recvStr = "";
                    recvStr += Encoding.ASCII.GetString(recvBytes, 0, iBytes);
                    Debug.Log("客户端获取的数据:" + recvStr);
                }
            }
            ///<summary>
            ///发送信息
            ///</summary>
            public static void SendMessage(string sendStr)
            {
                IPEndPoint ipe = (IPEndPoint)sTemp.RemoteEndPoint;
                sendStr = ipe.ToString() + sendStr;
                byte[] bs = Encoding.ASCII.GetBytes(sendStr);
                sTemp.Send(bs, bs.Length, 0);
            }
            ///<summary>
            ///关闭套接字
            ///</summary>
            public static void Close()
            {
                for (int i = 0; i < listPlayer.Count; i++)
                {
                    listPlayer[i].Close();
                }
                sTemp.Close();
                hSocket.Close();
            }
        }
    
    }
    

  • 相关阅读:
    Java单例模式深入详解
    深入理解Java的接口和抽象类
    java中为什么要给类使用代理?它有哪些好处?
    Log4j--java日志
    Log4j rootLogger配置
    selenium之 下拉选择框Select
    关于弹框
    spring controller接口中,用pojo对象接收页面传递的参数,发现spring在对pojo对象赋值时,有一定顺序的问题
    navicat mysql报错误:2013 Lost connection to MySQL server during query
    记录一次mysql导入千万条测试数据过慢的问题!
  • 原文地址:https://www.cnblogs.com/liang123/p/6325914.html
Copyright © 2011-2022 走看看