当前位置:网站首页>C# 34. UdpClient收发
C# 34. UdpClient收发
2022-06-09 18:16:00 【lljss2020】
//UdpClient.cs
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;
namespace Xxxxx
{
class UdpClient
{
private Socket client;
private string strIp;
private int port;
private byte[] msg;
public void UdpClientSocketCreat(string strIp,int port)
{
//创建客户端
client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPAddress ip = IPAddress.Parse(strIp);
///端口号
IPEndPoint endPoint = new IPEndPoint(ip, port);
///建立与服务器的远程连接
client.Connect(endPoint);
///线程问题
Thread thread = new Thread(ReciveMsg);
thread.IsBackground = true;
thread.Start(client);
Console.WriteLine("客户端已成功开启!");
}
//向特定ip的主机的端口发送数据
void SendMsg()
{
//获取IP与端口号
EndPoint point = new IPEndPoint(IPAddress.Parse(strIp), port);
//将数据发送到指定的ip的主机的端口
client.SendTo(msg, point);
}
public void Send(string strIp, int port, byte[] msg)
{
this.strIp = strIp;
this.port = port;
this.msg = msg;
//开启发送消息线程
Thread t1 = new Thread(SendMsg);
t1.Start();
}
//接收发送给本机ip对应端口号的数据
void ReciveMsg(object o)
{
Socket client = o as Socket;
while (true)
{
try
{
///用来保存发送方的ip和端口号
EndPoint point = new IPEndPoint(IPAddress.Any, 0);
///定义客户端接收到的信息大小
byte[] buffer = new byte[2000];
///接收到的信息大小(所占字节数)
int length = client.Receive(buffer);
}
catch (Exception)
{
client.Close();
}
}
}
}
}
//使用
UdpClient udpClient = new UdpClient();
byte[] buf = {
0x01,0x02,0x03,0x04,0x05};
udpClient.Send("192.168.90.100",3456, buf);
边栏推荐
- Acitivit的心路历程:Activiti6.0自定义任意跳转命令【包含回退】
- AI首席架构师3-AICA-智慧城市中的AI应用实践
- php使用unlink删除文件提示无权限
- mysql版本驱动问题
- What is the expected life of the conductive slip ring
- GCN图卷积神经网络概述
- Redis报错Field redisTemplate in xxx required a bean of type ‘org.x.RedisTemplate‘that can not be foud
- Go 最细节篇|pprof 统计的内存总是偏小?
- EasyCVR级联EasyCVR出现通道频繁掉线是什么原因?
- Deep understanding of JVM and GC
猜你喜欢

基于FPGA的SD卡读写设计及仿真verilog

Epigentek Hi-Fi cDNA 合成试剂盒说明书

Operating instructions for abbexa AEC chromogen Kit

Process control -- > > process termination

redis源码学习-01_Clion中调试redis源码

Interpretation of new shares | ranked second in the event content marketing industry, and wanted to push SaaS products on the cloud to create a competitive barrier

RPC framework in simple terms

Influence of K value selection in KNN on Model

微信小程序根据经纬度获取省市区信息

Zhichong launched net zero series energy storage and charging all-in-one machine to create a net zero future with BYD
随机推荐
直播预告 | 在阿里云 ESSD 云盘上部署 PolarDB for PostgreSQL 集群
EasyNVR非按需拉流返回的RTMP流地址无法播放如何处理?
Abbexa DUT ELISA 试剂盒测定原理
Epigentek染色质可及性检测试剂盒原则与程序
php使用unlink删除文件提示无权限
如何以案例学习kd树构建和搜索过程?
什么是波场TRX 钱包开发
Redis基础与高级
Go 最细节篇|pprof 统计的内存总是偏小?
js中性能优化之函数防抖
redis源码学习-03_动态字符串SDS
Influence of K value selection in KNN on Model
Overview of GCN graph convolution neural network
客户信息管理,业务场景1
redis源码学习-01_Clion中调试redis源码
How to build a real-time development platform to deeply release the value of enterprise real-time data?
详解MySQL索引
KNN中K值大小选择对模型的影响
Scala基本语法学习-1
【高等数学笔记】格林公式、高斯公式、斯托克斯公式、场论