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

  • 相关阅读:
    分苹果
    马拉车算法(求最长回文子串)
    KMP
    字典树
    关于子类和父类中的this的用法
    最长上生子序列LIS
    sass
    黑马程序员----java基础笔记下(毕向东)
    DOM
    ajax教程
  • 原文地址:https://www.cnblogs.com/liang123/p/6325913.html
Copyright © 2011-2022 走看看