转自: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. [系列] go-gin-api 路由中间件 - Jaeger 链路追踪(六)

    [DOC] 概述 首先同步下项目概况: 上篇文章分享了,路由中间件 - Jaeger 链路追踪(理论篇),这篇文章咱们接着分享:路由中间件 - Jaeger 链路追踪(实战篇). 这篇文章,确实让大家 ...

  2. 下载腾讯VIP视频

    1.找到自己想看的VIP视频网页地址,比如我就喜欢看一周一更的天行九歌,链接地址:https://v.qq.com/x/cover/rm3tmmat4li8uul/i0031xd1vjf.html 2 ...

  3. jquery 动态控制显隐

    1.第1种方法 ,给元素设置style属性 $("#hidediv").css("display", "block"); 2.第2种方法 , ...

  4. ajax 请求前后处理

    1. 介绍 通过 jQuery 提供的 ajaxSetup 方法,我们可以拦截页面上所有的 Ajax 请求响应(包括 $.ajax.$.post.$.get).这样我们可以对这些 Ajax 请求响应做 ...

  5. VS2010连接Oracle配置

    直接看上图.EZ连接和TNS连接.TNS连接要修改tnsnames.tns文件内部的host为服务器地址

  6. 洛谷:P5072 [Ynoi2015]盼君勿忘

    原题地址:https://www.luogu.org/problem/P5072 题目简述 给定一个序列,每次查询一个区间[l,r]中所有子序列分别去重后的和mod p 思路 我们考虑每个数的贡献.即 ...

  7. windows下python和pycharm安装及其使用

    1.python安装及环境变量配置 1.1 python安装 1.1.1 python下载 官网下载:https://www.python.org/ Downloads-Windows(Mac os ...

  8. java基础之泛型对象与json互转

    1. 场景描述 把泛型对象转成字符串放到缓存中,获取后使用有点问题,记录下,有碰到的朋友,参考下. 2. 解决方案 2.1 操作类及说明 /** * @auther: 软件老王 */ public s ...

  9. 利用js种的正则删除html标签

    public static string NoHTML(string Htmlstring) { //删除脚本 Htmlstring = Regex.Replace(Htmlstring, @&quo ...

  10. python编程基础之二十二

    字典:字典属于可变对象,但是不属于序列,内部是通过哈希方式存储的,内部保存的是一个个键值对key:value 字典的键是唯一的, 字典查找速度比较快 d1 = {}  #括号里面用键值对表示 d2 = ...