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时尤其成问题,其需要在同一 ...
随机推荐
- java 测试框架
项目开发过程中使用的单元测试框架有Junit.TestNG以及Mockito,Junit和TestNG使用的比较多,Mockito最近才开始使用. TestNG与JUnit的相同点 1. 使用anno ...
- 安装RabbitMQ管理插件失败
运行 rabbitmq-plugins.bat enable rabbitmq_management后提示失败信息 是因为erlang和RabbitMQ版本冲突导致
- php判断为空就插入,判断不为空就更新
if ($_GET['tplname']!==null) { if ($userinfo[0] == ''){$exec="INSERT INTO cblej_company_pc_temp ...
- .Netcore 2.0 Ocelot Api网关教程(9)- QoS
本文介绍Ocelot中的QoS(Quality of Service),其使用了Polly对超时等请求下游失败等情况进行熔断. 1.添加Nuget包 添加 Ocelot.Provider.Polly ...
- Run Hyper-V and VirtualBox on the same machine (轉載)
Run Hyper-V and VirtualBox on the same machine Posted on September 5, 2012 by derek gusoff Recently ...
- (IStool)软件打包时当文件存在时不覆盖文件(配置文件)
需求:程序实际使用过程中有些配置信息是需要用户手动配置的,不同客户使用配置信息也不同,所以软件发布前需要考虑这个问题,覆盖安装时需要忽略这些配置文件 实现:当对应的目录下由此文件的时候不覆盖此文件 [ ...
- WhatsApp Group vs WhatsApp Broadcast for Business
WhatsApp Group vs WhatsApp Broadcast for Business By Iaroslav Kudritskiy If you've read our Ultimate ...
- 分布式消息通信之RabbitMQ Tutorials
目录 官网 1 Hello World! 1.1 生产者demo producer 1.2 消费者demo consumer 1.3 查看queue队列中的信息 页面查看,可看到有4条消息 命令查看 ...
- 【FFMPEG】ffmpeg 中添加264支持
ffmpeg 中带有264的解码,没有编码,需要添加x264: 参考百度上的"windows_ffmpeg编译 " 文档 下载ffmpeg 代码合x264代码, ffmpeg 代码 ...
- idea把maven依赖树输出到控制台
第一步 选中红色方框 第二步 点进去 输入命令:mvn dependency:tree 如果要输出到文件,找到pom文件的位置 进入命令行 输入: mvn dependency:tree >d: ...