zoukankan      html  css  js  c++  java
  • AsyncUdpClient 类

    View Code
    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;

    namespace Rocky.Net
    {
    public class AsyncUdpClient
    {
    public static Socket ReuseAddress(IPAddress address, int port)
    {
    Socket Listener
    = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    Listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress,
    1);
    Listener.Bind(
    new IPEndPoint(IPAddress.Any, port));
    Listener.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
    new MulticastOption(address));
    return Listener;
    }

    public event AsyncUdpEventHandler Sent;
    public event AsyncUdpEventHandler Received;

    private byte[] buffer;
    private bool isListening;
    private IPEndPoint listenEndPoint;
    private Socket sock;

    public bool IsListening
    {
    get { return isListening; }
    }
    public IPEndPoint ListenEndpoint
    {
    get { return listenEndPoint; }
    }
    public Socket Client
    {
    get { return sock; }
    }

    public AsyncUdpClient()
    {
    buffer
    = new byte[BufferUtility.DefaultBufferSize];
    sock
    = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    }
    public AsyncUdpClient(int port)
    {
    listenEndPoint
    = new IPEndPoint(IPAddress.Any, port);
    }

    public void StartListen()
    {
    if (!isListening)
    {
    if (!sock.IsBound)
    {
    sock.Bind(listenEndPoint);
    }
    IPEndPoint epSender
    = new IPEndPoint(IPAddress.Any, 0);
    EndPoint epRemote
    = (EndPoint)epSender;
    sock.BeginReceiveFrom(buffer,
    0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(ReceiveCallBack), epRemote);
    isListening
    = true;
    }
    }

    public void StopListen()
    {
    isListening
    = false;
    }

    public void Send(IPEndPoint epRemote, byte[] data)
    {
    sock.BeginSendTo(data,
    0, data.Length, SocketFlags.None, epRemote, new AsyncCallback(SendCallBack), epRemote);
    }

    public void Close()
    {
    sock.Close(
    200);
    }

    private void ReceiveCallBack(IAsyncResult ar)
    {
    EndPoint epRemote
    = (EndPoint)ar.AsyncState;
    int recv = sock.EndReceiveFrom(ar, ref epRemote);
    if (Received != null)
    {
    Received(
    this, new AsyncUdpEventArgs(epRemote, buffer));
    }
    if (isListening)
    {
    sock.BeginReceiveFrom(buffer,
    0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(ReceiveCallBack), epRemote);
    }
    }
    private void SendCallBack(IAsyncResult ar)
    {
    EndPoint epRemote
    = (EndPoint)ar.AsyncState;
    int sent = sock.EndSend(ar);
    if (Sent != null)
    {
    Sent(
    this, new AsyncUdpEventArgs(epRemote, buffer));
    }
    }
    }
    }
    View Code
    using System;
    using System.Net;

    namespace Rocky.Net
    {
    public delegate void AsyncUdpEventHandler(object sender, AsyncUdpEventArgs e);

    public class AsyncUdpEventArgs : EventArgs
    {
    private EndPoint _remoteEndPoint;
    private byte[] _data;

    public EndPoint RemoteEndPoint
    {
    get { return _remoteEndPoint; }
    }
    public byte[] Data
    {
    get { return _data; }
    }

    public AsyncUdpEventArgs(EndPoint remoteEndPoint, byte[] data)
    {
    _remoteEndPoint
    = remoteEndPoint;
    _data
    = data;
    }
    }
    }
  • 相关阅读:
    webuploader多次触发注册
    下载二进制文件
    表格打印
    多个请求下 loading 的展示与关闭
    前进刷新后退不刷新
    页面权限控制和登陆验证
    axios.post 配置formdata提交
    react错误边界
    关于beforeRouterEnter获取不到this
    JS
  • 原文地址:https://www.cnblogs.com/Googler/p/1988999.html
Copyright © 2011-2022 走看看