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();
        }
       
    
    }
    

  • 相关阅读:
    onkeydown事件
    单击循环事件
    for-in循环
    in运算符
    数组成员升序降序排列
    bzoj 3754: Tree之最小方差树 模拟退火+随机三分
    bzoj 3752: Hack 预处理+暴力dfs
    hdu 5269 ZYB loves Xor I 分治 || Trie
    bzoj 4501: 旅行 01分数规划+概率期望dp
    bzoj 4260: REBXOR Trie+乱搞
  • 原文地址:https://www.cnblogs.com/liang123/p/6325913.html
Copyright © 2011-2022 走看看