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

    using System.Collections;
    using System.Collections.Generic;
    using System.Net.Sockets;
    using System.Net;
    using System.Threading;
    using System.Text;
    
    
    public class SocketUDPServer
    {
        private string ip = "127.0.0.1";
        private int port = 5690;
        private Socket socket;
        private static SocketUDPServer socketServer;
        public List<string> listMessage = new List<string>();
    
        public static SocketUDPServer getInstance()
        {
            if (socketServer == null)
            {
                socketServer = new SocketUDPServer();
                socketServer.Init();
            }
            return socketServer;
        }
    
        private void Init()
        {
            IPAddress ipAddress = IPAddress.Parse(ip);
            IPEndPoint IPE = new IPEndPoint(ipAddress,port);
            //Udp搭配SocketType.Dgram   Tcp搭配SocketType.Stream
            socket=new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
            socket.Bind(IPE);
            Thread threadReceive = new Thread(new ThreadStart(ReceiveMessage));
            threadReceive.Start();
        }
        private void ReceiveMessage()
        {
            while (true)
            {
                byte[] buff = new byte[1024];
                int iBytes = socket.Receive(buff, SocketFlags.None);
                if (iBytes <= 0)
                    break;
                string strGetMessage = Encoding.ASCII.GetString(buff, 0, iBytes);
                listMessage.Add(strGetMessage);
            }
        }
        public void  Close()
        {
            if(socket!=null)
                socket.Close();
        }
       
    
    }
    

  • 相关阅读:
    mongodb安装
    node版本的管理 n
    npm 命令
    nodejs,npm安装(ubuntu14.04下)
    yeoman,grunt,bower安装(ubuntu14.04)
    什么是堆和栈,它们在哪儿?
    malloc函数详解 (与new对比)
    单链表的C++实现(采用模板类)
    短信验证码
    webapi
  • 原文地址:https://www.cnblogs.com/liang123/p/6325913.html
Copyright © 2011-2022 走看看