转自:https://blog.csdn.net/zhujunxxxxx/article/details/44258719

1)服务端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text; namespace UdpSocketServer
{
public class UdpServerAsync
{
#region Fields private int _maxClient; private Socket _serverSock; private List<EndPoint> _clients; private bool disposed = false; private byte[] _recvBuffer; #endregion #region Properties public bool IsRunning { get; private set; } public IPAddress Address { get; private set; } public int Port { get; private set; } public Encoding Encoding { get; set; } #endregion #region 构造函数 public UdpServerAsync(int listenPort)
: this(IPAddress.Any, listenPort, )
{
} public UdpServerAsync(IPEndPoint localEP)
: this(localEP.Address, localEP.Port, )
{
} public UdpServerAsync(IPAddress localIPAddress, int listenPort, int maxClient)
{
this.Address = localIPAddress;
this.Port = listenPort;
this.Encoding = Encoding.Default; _maxClient = maxClient; _serverSock = new Socket(localIPAddress.AddressFamily, SocketType.Dgram, ProtocolType.Udp); _recvBuffer = new byte[_serverSock.ReceiveBufferSize];
} #endregion #region Method public void Start()
{
if (!IsRunning)
{
IsRunning = true;
_serverSock.Bind(new IPEndPoint(this.Address, this.Port));
//_serverSock.Connect(new IPEndPoint(IPAddress.Any, 0)); SocketState so = new SocketState();
so.buffer = new byte[_serverSock.ReceiveBufferSize];
so.remote = new IPEndPoint(Address, Port); _serverSock.BeginReceiveFrom(so.buffer, , so.buffer.Length, SocketFlags.None,
ref so.remote, new AsyncCallback(ReceiveDataAsync), so);
}
} public void Stop()
{
if (IsRunning)
{
IsRunning = false;
_serverSock.Close();
}
} private void ReceiveDataAsync(IAsyncResult ar)
{
SocketState so = ar.AsyncState as SocketState; int len = -;
try
{
len = _serverSock.EndReceiveFrom(ar, ref so.remote);
so.recvSize = len;
//len = _serverSock.EndReceiveFrom(ar, ref sender); //EndReceiveFrom 和 EndReceive区别
//len = _serverSock.EndReceive(ar);
//TODO 处理数据 //触发数据收到事件
RaiseDataReceived(so);
}
catch (Exception)
{
//TODO 处理异常
RaiseOtherException(so);
}
finally
{
if (IsRunning && _serverSock != null)
_serverSock.BeginReceiveFrom(so.buffer, , so.buffer.Length, SocketFlags.None,
ref so.remote, new AsyncCallback(ReceiveDataAsync), so);
}
} public void Send(byte[] data, EndPoint remote)
{
try
{
RaisePrepareSend(null);
_serverSock.BeginSendTo(data, , data.Length, SocketFlags.None, remote, new AsyncCallback(SendDataEnd), _serverSock);
}
catch (Exception)
{
//TODO 异常处理
RaiseOtherException(null);
}
} private void SendDataEnd(IAsyncResult ar)
{
((Socket)ar.AsyncState).EndSendTo(ar);
RaiseCompletedSend(null);
} #endregion #region 事件 public event EventHandler<UDPMessage> DataReceived; private void RaiseDataReceived(SocketState state)
{
if (DataReceived != null)
{
DataReceived(this, new UDPMessage(state));
}
} /// <summary>
/// 发送数据前的事件
/// </summary>
public event EventHandler<UDPMessage> PrepareSend; /// <summary>
/// 触发发送数据前的事件
/// </summary>
/// <param name="state"></param>
private void RaisePrepareSend(SocketState state)
{
if (PrepareSend != null)
{
PrepareSend(this, new UDPMessage(state));
}
} /// <summary>
/// 数据发送完毕事件
/// </summary>
public event EventHandler<UDPMessage> CompletedSend; /// <summary>
/// 触发数据发送完毕的事件
/// </summary>
/// <param name="state"></param>
private void RaiseCompletedSend(SocketState state)
{
if (CompletedSend != null)
{
CompletedSend(this, new UDPMessage(state));
}
} /// <summary>
/// 网络错误事件
/// </summary>
public event EventHandler<UDPMessage> NetError;
/// <summary>
/// 触发网络错误事件
/// </summary>
/// <param name="state"></param>
private void RaiseNetError(SocketState state)
{
if (NetError != null)
{
NetError(this, new UDPMessage(state));
}
} /// <summary>
/// 异常事件
/// </summary>
public event EventHandler<UDPMessage> OtherException;
/// <summary>
/// 触发异常事件
/// </summary>
/// <param name="state"></param>
private void RaiseOtherException(SocketState state, string descrip)
{
if (OtherException != null)
{
OtherException(this, new UDPMessage(descrip, state));
}
}
private void RaiseOtherException(SocketState state)
{
RaiseOtherException(state, "");
}
#endregion #region Close
/// <summary>
/// 关闭一个与客户端之间的会话
/// </summary>
/// <param name="state">需要关闭的客户端会话对象</param>
public void Close(SocketState state)
{
if (state != null)
{
//_clients.Remove(state);
//_clientCount--;
//TODO 触发关闭事件
}
}
/// <summary>
/// 关闭所有的客户端会话,与所有的客户端连接会断开
/// </summary>
public void CloseAllClient()
{
//foreach (AsyncUDPSocketState client in _clients)
//{
// Close(client);
//}
//_clientCount = 0;
//_clients.Clear();
} #endregion #region 释放
/// <summary>
/// Performs application-defined tasks associated with freeing,
/// releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
} /// <summary>
/// Releases unmanaged and - optionally - managed resources
/// </summary>
/// <param name="disposing"><c>true</c> to release
/// both managed and unmanaged resources; <c>false</c>
/// to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
try
{
Stop();
if (_serverSock != null)
{
_serverSock = null;
}
}
catch (SocketException)
{
//TODO
RaiseOtherException(null);
}
}
disposed = true;
}
}
#endregion
}
}

2)客户端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text; namespace UdpSocketClient
{
public class UDPClientAsync
{
#region 字段 public Socket udpClient; public EndPoint endPoint; private SocketState state; #endregion #region 属性 public IPAddress Address { get; private set; } public int Port { get; private set; } #endregion #region 构造函数 public UDPClientAsync(IPAddress ipAddress,int port)
{
this.Address = ipAddress;
this.Port = port;
endPoint = new IPEndPoint(Address, Port);
udpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
} public UDPClientAsync(IPEndPoint ipEndPoint)
:this(ipEndPoint.Address,ipEndPoint.Port)
{
} #endregion #region Method public void Send(byte[] bytes)
{
udpClient.BeginSendTo(bytes, , bytes.Length, SocketFlags.None, endPoint, iAsyncResult=>
{
udpClient.EndSendTo(iAsyncResult); RaiseDataSend();
}, null);
} public void Receive()
{
state = new UdpSocketClient.SocketState();
state.buffer = new byte[udpClient.ReceiveBufferSize];
state.remote = new IPEndPoint(Address, Port); udpClient.BeginReceiveFrom(state.buffer, , state.buffer.Length, SocketFlags.None, ref state.remote,
new AsyncCallback(DataReceivedCallBack), null);
} private void DataReceivedCallBack(IAsyncResult iAsyncResult)
{
int len = -;
try
{
len = udpClient.EndReceiveFrom(iAsyncResult, ref state.remote);
state.recvSize = len; RaiseDataReceived(state);
}
catch(Exception ex)
{
RaiseErrorException(ex.Message);
} //if (udpClient != null)
//{
// udpClient.BeginReceiveFrom(state.buffer, 0, state.buffer.Length, SocketFlags.None, ref state.remote,
// new AsyncCallback(DataReceivedCallBack), null);
//}
} public void Close()
{
udpClient.Shutdown(SocketShutdown.Both); udpClient.Close();
GC.Collect();
} #endregion #region 事件 public event EventHandler<UDPMessage> DataReceived; private void RaiseDataReceived(SocketState state)
{
if(DataReceived!=null)
{
DataReceived(this, new UdpSocketClient.UDPMessage(state));
}
} public event EventHandler<UDPMessage> DataSend; private void RaiseDataSend()
{
if(DataSend!=null)
{
DataSend(this, null);
}
} public event EventHandler<UDPMessage> ErrorException; private void RaiseErrorException(string msg)
{
if(ErrorException!=null)
{
ErrorException(this, new UdpSocketClient.UDPMessage(msg));
}
} #endregion
}
}

3)消息类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text; namespace UdpSocketServer
{
public class SocketState
{
public Socket workSocket = null; public byte[] buffer; public int recvSize; public EndPoint remote; public string Diagram
{
get
{
byte[] data = new byte[recvSize];
Array.ConstrainedCopy(buffer, , data, , recvSize);
//_datagram = Encoding.Default.GetString(data);
return Encoding.Default.GetString(data);
}
}
}
}

4)服务端信息类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UdpSocketClient
{
    public class UDPMessage : EventArgs
    {
        /// <summary> 
        /// 提示信息 
        /// </summary> 
        public string _msg;
        /// <summary> 
        /// 客户端状态封装类 
        /// </summary> 
        public SocketState _state;
        /// <summary> 
        /// 是否已经处理过了 
        /// </summary> 
        public bool IsHandled { get; set; }
        public UDPMessage(string msg)
        {
            this._msg = msg;
            IsHandled = false;
        }
        public UDPMessage(SocketState state)
        {
            this._state = state;
            IsHandled = false;
        }
        public UDPMessage(string msg, SocketState state)
        {
            this._msg = msg;
            this._state = state;
            IsHandled = false;
        }
    }
}

Udp 异步通信(三)的更多相关文章

  1. TCP/IP协议 | TCP协议 | UDP协议 | 三次握手四次挥手

    TCP/IP协议不仅仅指的是TCP 和IP两个协议,而是指一个由FTP.SMTP.TCP.UDP.IP等协议构成的协议簇, 只是因为在TCP/IP协议中TCP协议和IP协议最具代表性,所以被称为TCP ...

  2. UDP异步通信

    先看效果图 Server: using System; using System.Collections.Generic; using System.Text; using System.Net; u ...

  3. C#UDP异步通信

    using SetingDemo.LogHelp;using SetingDemo.SingleRowDeclare;using System;using System.Collections.Gen ...

  4. 基于socket的TCP和UDP编程

    一.概述 TCP(传输控制协议)和UDP(用户数据报协议是网络体系结构TCP/IP模型中传输层一层中的两个不同的通信协议. TCP:传输控制协议,一种面向连接的协议,给用户进程提供可靠的全双工的字节流 ...

  5. (转)基于socket的TCP和UDP编程

    一.概述 TCP(传输控制协议)和UDP(用户数据报协议是网络体系结构TCP/IP模型中传输层一层中的两个不同的通信协议. TCP:传输控制协议,一种面向连接的协议,给用户进程提供可靠的全双工的字节流 ...

  6. UDP 打洞 原理解释

    终于找到了一份满意的UDP打洞原理解释,附上正文,自己整理了一下源码 3.3. UDP hole punching UDP打洞技术 The third technique, and the one o ...

  7. 初识-----基于Socket的UDP和TCP编程及测试代码

    一.概述 TCP(传输控制协议)和UDP(用户数据报协议是网络体系结构TCP/IP模型中传输层一层中的两个不同的通信协议. TCP:传输控制协议,一种面向连接的协议,给用户进程提供可靠的全双工的字节流 ...

  8. 基于Socket的UDP和TCP编程介绍

    一.概述 TCP(传输控制协议)和UDP(用户数据报协议是网络体系结构TCP/IP模型中传输层一层中的两个不同的通信协议. TCP:传输控制协议,一种面向连接的协议,给用户进程提供可靠的全双工的字节流 ...

  9. socket http tcp udp ip 协议

    Socket可以支持不同的传输层协议(TCP或UDP),当使用TCP协议进行连接时,该Socket连接就是一个TCP连接. socket是在应用层和传输层之间的一个抽象层,它把TCP/IP层复杂的操作 ...

随机推荐

  1. 为何stop()和suspend()方法不推荐使用(转)

    stop()方法作为一种粗暴的线程终止行为,在线程终止之前没有对其做任何的清除操作,因此具有固有的不安全性. 用Thread.stop()方法来终止线程将会释放该线程对象已经锁定的所有监视器.如果以前 ...

  2. ViewModel 和 ViewModelProvider.Factory:ViewModel 的创建者

    本文翻译自:https://medium.com/koderlabs/viewmodel-with-viewmodelprovider-factory-the-creator-of-viewmodel ...

  3. linux mint 17编译android 2.3.1错误记录

    有转载这里的也有添加的. ################# Fix 1 ########################## Error: frameworks/base/include/utils ...

  4. mysql 查找数组格式的字符串中是否包含某个值

    --#{type}表示将判断的单个值 例如 -- arr表示数组格式的字符串,例如 ,,, FIND_IN_SET(#{type},arr) 使用的时候,举例:

  5. MongoDB 基础教程CURD帮助类

    最近两天在学习MongoDB,强大的文档数据库.给我最大的感觉就是相比于SQL或者MSQ等传统的关系型数据库,在使用和配置上真的是简化了很多.无论是在集群的配置还是故障转移方面,都省去了许多繁琐的步骤 ...

  6. xpath语法分享

    # xpath语法: ## 使用方式: 使用//获取整个页面当中的元素,然后写标签名,然后再写谓词进行提取.比如: ``` //div[@class='abc'] ``` ## 需要注意的知识点: 1 ...

  7. ng执行css3动画

    在组件html中 <div> <aside id="aside">侧边栏</aside> <div class="content ...

  8. Redis未授权访问漏洞复现

    Redis未授权访问漏洞复现 一.漏洞描述 Redis默认情况下,会绑定在0.0.0.0:6379(在redis3.2之后,redis增加了protected-mode,在这个模式下,非绑定IP或者没 ...

  9. 自己写的Weblogic的poc

    """ 暂时只试用于Linux,先试试用一下反弹shell CVE-2017-10271的EXp """ import requests i ...

  10. 解决window.onload延迟加载问题

    window.onload方法,表示当页面所有的元素都加载完毕,并且所有要请求的资源也加载完毕才触发执行function这个匿名函数里边的具体内容.这样肯定保证了代码在domReady之后执行.使用w ...