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;
    }
    }
    }
  • 相关阅读:
    Python通过多线程实现 `异步`
    Linux(六) 处理用户输入
    Linux(五) 更多结构化命令
    Linux(四) 使用结构化命令
    Linux(三) 科学计算
    Linux(二) Shell脚本
    python 登陆接口
    学习的小建议
    干货
    ThinkPhp5 自定义异常处理类
  • 原文地址:https://www.cnblogs.com/Googler/p/1988999.html
Copyright © 2011-2022 走看看