unity:unity Scket 简单的Udp连接 复制就能用

// 本文是C# 脚本,用的时候new 下就行

using UnityEngine;
using System.Collections;
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;

///


///UDP  Socket 
///

public class State
{
    public Socket _workSocket;
    public const int _bufferSize = 1024;
    public byte[] _buffer = new byte[_bufferSize];
    public EndPoint _renmoteEp  = new IPEndPoint(IPAddress.Any,0);
}

public class SingleUdpClient  
{
    public static SingleUdpClient _myUdpClient;
    private Socket _clientSocket ;
    public static SingleUdpClient GetInstance()
    {
        if(_myUdpClient == null)
        {
            _myUdpClient = new SingleUdpClient();
        }
        return _myUdpClient;
    }
    public SingleUdpClient()
    {
        _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,ProtocolType.Udp);

        //client
        IPEndPoint localEP = new IPEndPoint(IPAddress.Any, 5001);
        _clientSocket.Bind(localEP);

        State _state = new State();
        _state._workSocket = _clientSocket;

//        
        //EndPoint remoteEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6000);
        //SendMeaage(_clientSocket, remoteEP, "你好,这里是客户端,收到请回复!Over~~~~");
        _clientSocket.BeginReceiveFrom(_state._buffer,0,State._bufferSize,SocketFlags.None,ref _state._renmoteEp,new AsyncCallback(ReceiverCallBack),_state);
    }
    // 接受数据
    void ReceiverCallBack(IAsyncResult _ar)
    {
        State _state = (State) _ar.AsyncState;
        Socket _myClientSocket = _state._workSocket;

        try
        {
            //完成接收
            int _dataLength = _myClientSocket.EndReceiveFrom(_ar, ref _state._renmoteEp);

            //显示客户端地址和端口
            //Debug.Log("服务器终结点:"+ _state._renmoteEp.ToString());
            //显示接收信息
            //Debug.Log("接收数据字节数:"+ _dataLength);

            string _dataReceive = Encoding.Default.GetString(_state._buffer, 0, _dataLength);// 接收数据
            //Pvoe._instance.Synchronization(_dataReceive);///接收数据进行使用

        }
        catch (Exception e)
        {
            Debug.Log("发生异常!异常信息:"+e);
        }
        finally
        {
            //非常重要:continue 异步接收
            _myClientSocket.BeginReceiveFrom(_state._buffer, 0, State._bufferSize,SocketFlags.None,ref _state._renmoteEp,new AsyncCallback( ReceiverCallBack),_state);
        }
    }
    // 发送数据
    void SendContentMessage(Socket socket, EndPoint remoteEndPoint, string Message)
    {
        

        byte[] bytes = Encoding.UTF8.GetBytes(Message);
        socket.SendTo(bytes, remoteEndPoint);
    }

    public void SendContentMessage( string _ipAddress,string _data,int port)
    {
        EndPoint remoteEP = new IPEndPoint(IPAddress.Parse(_ipAddress), port);
        SendContentMessage(_clientSocket, remoteEP, _data);
    }
    public void SendContentMessage(string _data)
    {
        byte[] bytes = Encoding.Default.GetBytes(_data);
        _clientSocket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.Broadcast,1);
        EndPoint iep = new IPEndPoint(IPAddress.Broadcast,60000);
        _clientSocket.SendTo(bytes,iep);
    }
    
    public string ReceiveData(string _data) 
    { Debug.Log(_data); return _data; } }

你可能感兴趣的:(unity:unity Scket 简单的Udp连接 复制就能用)