C#UDP异步通信
using SetingDemo.LogHelp;
using SetingDemo.SingleRowDeclare;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
namespace SetingDemo.SocketHelp
{
public class UdpState
{
public UdpClient udpClient;
public IPEndPoint ipEndPoint;
public const int BufferSize = 1024;
public byte[] buffer = new byte[BufferSize];
public int counter = 0;
}
public class AsyncUdpSever
{ // 定义节点
private IPEndPoint ipEndPoint = null;
private IPEndPoint remoteEP = null;
// 定义UDP发送和接收
private UdpClient udpReceive = null;
private UdpClient udpSend = null;
// 定义端口
private const int listenPort = 60091;
private const int remotePort = 60091;
UdpState udpReceiveState = null;
UdpState udpSendState = null;
// 异步状态同步
private ManualResetEvent sendDone = new ManualResetEvent(false);
private ManualResetEvent receiveDone = new ManualResetEvent(false);
public AsyncUdpSever()
{
}
public void ReceiveMsg()
{
while (true)
{
lock (this)
{
// 调用接收回调函数
IAsyncResult iar = udpReceive.BeginReceive(new AsyncCallback(ReceiveCallback), udpReceiveState);
receiveDone.WaitOne();
Thread.Sleep(100);
}
}
}
// 接收回调函数
private void ReceiveCallback(IAsyncResult iar)
{
UdpState udpReceiveState = iar.AsyncState as UdpState;
if (iar.IsCompleted)
{
Byte[] receiveBytes = udpReceiveState.udpClient.EndReceive(iar, ref udpReceiveState.ipEndPoint);
string receiveString = Encoding.ASCII.GetString(receiveBytes);
Task.Run(() =>
{
Application.Current.Dispatcher.Invoke(() =>
{
CKInstanceBase<ReviceMsgClass>.Instance.AddMsg(string.Concat("Udp接收:", receiveString));
});
});
Console.WriteLine("UDP-Received: {0}", receiveString);
//Thread.Sleep(100);
receiveDone.Set();
}
}
// 发送函数
private void SendMsg()
{
udpSend.Connect(udpSendState.ipEndPoint);
udpSendState.udpClient = udpSend;
udpSendState.counter++;
string message = string.Format("第{0}个UDP请求处理完成!", udpSendState.counter);
Byte[] sendBytes = Encoding.Unicode.GetBytes(message);
udpSend.BeginSend(sendBytes, sendBytes.Length, new AsyncCallback(SendCallback), udpSendState);
sendDone.WaitOne();
}
// 发送回调函数
private void SendCallback(IAsyncResult iar)
{
UdpState udpState = iar.AsyncState as UdpState;
Console.WriteLine("第{0}个请求处理完毕!", udpState.counter);
Console.WriteLine("number of bytes sent: {0}", udpState.udpClient.EndSend(iar));
sendDone.Set();
}
public void StatUdpRecv()
{
// 本机节点
ipEndPoint = new IPEndPoint(IPAddress.Any, listenPort);
// 远程节点
remoteEP = new IPEndPoint(Dns.GetHostAddresses(Dns.GetHostName())[0], remotePort);
// 实例化
udpReceive = new UdpClient(ipEndPoint);
udpSend = new UdpClient();
// 分别实例化udpSendState、udpReceiveState
udpReceiveState = new UdpState();
udpReceiveState.udpClient = udpReceive;
udpReceiveState.ipEndPoint = ipEndPoint;
udpSendState = new UdpState();
udpSendState.udpClient = udpSend;
udpSendState.ipEndPoint = remoteEP;
Thread t = new Thread(new ThreadStart(ReceiveMsg));
t.IsBackground = true;
t.Start();
Console.Read();
}
}
}
C#UDP异步通信的更多相关文章
- UDP异步通信
先看效果图 Server: using System; using System.Collections.Generic; using System.Text; using System.Net; u ...
- Udp 异步通信(三)
转自:https://blog.csdn.net/zhujunxxxxx/article/details/44258719 1)服务端 using System; using System.Colle ...
- JAVA NIO异步通信框架MINA选型和使用的几个细节(概述入门,UDP, 心跳)
Apache MINA 2 是一个开发高性能和高可伸缩性网络应用程序的网络应用框架.它提供了一个抽象的事件驱动的异步 API,可以使用 TCP/IP.UDP/IP.串口和虚拟机内部的管道等传输方式.A ...
- C# Socket编程 同步以及异步通信
套接字简介:套接字最早是Unix的,window是借鉴过来的.TCP/IP协议族提供三种套接字:流式.数据报式.原始套接字.其中原始套接字允许对底层协议直接访问,一般用于检验新协议或者新设备问题,很少 ...
- C# HTTP1.0 1.1 2.0与HTTPS 、TCP/IP协议的UDP与TCP、 Socket介绍与WebSocket
一.HTTP1.0 1.1 2.0和HTTPS 1.HTTP协议是什么? HTTP协议是超文本传输协议的缩写,英文是Hyper Text Transfer Protocol.它是从WEB服务器传输超文 ...
- UDP协议,多道技术,进程,同步与异步,阻塞与非阻塞
UDP协议 简介 UDP叫做用户数据报协议,是OSI七层参考模型中传输层使用的协议,他提供的是不可靠传输,既它在传输过程 中不保证数据的完整性! 端口号 UDP使用IP地址和端口号进行标识,以此将数据 ...
- Node.js:dgram模块实现UDP通信
1.什么是UDP? 这里简单介绍下,UDP,即用户数据报协议,一种面向无连接的传输层协议,提供不可靠的消息传送服务.UDP协议使用端口号为不同的应用保留其各自的数据传输通道,这一点非常重要.与TCP相 ...
- 高性能 TCP/UDP/HTTP 通信框架 HP-Socket v4.1.1
HP-Socket 是一套通用的高性能 TCP/UDP/HTTP 通信框架,包含服务端组件.客户端组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP/HTTP 通信系统,提供 C/ ...
- TODO:Golang语言TCP/UDP协议重用地址端口
TODO:Golang语言TCP/UDP协议重用地址端口 这是一个简单的包来解决重用地址的问题. go net包(据我所知)不允许设置套接字选项. 这在尝试进行TCP NAT时尤其成问题,其需要在同一 ...
随机推荐
- ubuntu下安装g++
主要来自ubuntu中文社区http://www.ubuntu.org.cn/support/documentation/doc/VMware 首选,确认你已经安装了build-essential程序 ...
- Flutter移动电商实战 --(17)首页_楼层区域的编写
1.楼层标题组件 该组件非常简单,只接收一个图片地址,然后显示即可: class FloorTitle extends StatelessWidget { final String picture_a ...
- P1944 最长括号匹配_NOI导刊2009提高(1)
P1944 最长括号匹配_NOI导刊2009提高 题解 宁愿相信世上有鬼,也不能随便相信某谷题目标签 我想了半天然后看了眼题解,发现用栈来模拟就好了 栈来模拟,还要用到一个bool数组,标记是否已经匹 ...
- Visual Studio Team Systems
https://www.cnblogs.com/33568639/archive/2008/12/29/1364222.html https://baike.sogou.com/v7818386.ht ...
- 美国top200药品2
python机器学习-乳腺癌细胞挖掘(博主亲自录制视频)https://study.163.com/course/introduction.htm?courseId=1005269003&u ...
- 使用sproto buff 的陷阱
当sproto协议包中的数组元素,长度为0时,会出现接收异常.在没有调试断点的情况下,会停止接收其它协议.
- ID3算法(MATLAB)
ID3算法是一种贪心算法,用来构造决策树.ID3算法起源于概念学习系统(CLS),以信息熵的下降速度为选取测试属性的标准,即在每个节点选取还尚未被用来划分的具有最高信息增益的属性作为划分标准,然后继续 ...
- 物联网安全himqtt防火墙数据结构之ringbuffer环形缓冲区
物联网安全himqtt防火墙数据结构之ringbuffer环形缓冲区 随着5G的普及,物联网安全显得特别重要,himqtt是首款完整源码的高性能MQTT物联网防火墙 - MQTT Applicatio ...
- Vue的作用域插槽
一.通常情况下都是父组件传递数据给子组件进行展示的(无法改变子组件的展示方式):而作用域插槽允许子组件通过slot向父组件传递数据,类似React中的“以函数为子组件”,由父组件决定渲染的内容(包含绑 ...
- [转帖]从入门到实践:创作一个自己的 Helm Chart
从入门到实践:创作一个自己的 Helm Chart https://www.cnblogs.com/alisystemsoftware/p/11436469.html 自己已经搭建好了 helm 和t ...