UDP通信程序C#入门示例

UDP 通信的 C#实现挺常见的,用起来也还算顺手。UDP 通信程序.rar 这个压缩包里就是一套比较实用的入门示例,包含了服务器和客户端的完整代码,适合你快速上手测试。

UdpClient这个类在 C#里用来搞 UDP 通信还挺方便的。你只需要几行代码就能发数据、收数据,像client.Send()client.Receive()这些方法都蛮直观的,响应也快。

UDP 嘛,传输快、不管顺序也不保证到达,适合做实时性要求高的东西,比如局域网聊天、视频预览、游戏同步这些。如果你用过TCPClient,会发现 UDP 更“野”一点,但也轻巧得多。

压缩包里率有些BeginReceiveReceiveAsync的异步写法,还顺带讲了下多线程,适合你搞个小 Demo 玩一下异步接收或者并发场景。不想被阻塞卡着,这招必须会。

要注意的就是:异常得做好,SocketException偶尔会蹦,尤其在收发数据的时候。再一个就是 IP 和端口这些配好,是用IPEndPoint发数据前。

顺带推荐几个文章,有空可以看看:

如果你正好要做个轻量级的网络通信功能,这套代码解压完看看就能跑,蛮省事的。

rar
UDP通信程序.rar 预估大小:36个文件
folder
UDP通信程序 文件夹
folder
UDP通信程序 文件夹
file
UDP通信程序.csproj.user 452B
file
Form1.cs 5KB
folder
bin 文件夹
folder
Debug 文件夹
file
UDP通信程序.exe.manifest 4KB
file
UDP通信程序.application 2KB
file
UDP通信程序.vshost.exe.config 117B
file
UDP通信程序.vshost.application 2KB
file
UDP通信程序.vshost.exe.manifest 4KB
file
UDP通信程序.vshost.exe 11KB
file
UDP通信程序.pdb 24KB
file
UDP通信程序.exe 12KB
file
UDP通信程序.exe.config 117B
folder
Release 文件夹
folder
obj 文件夹
folder
x86 文件夹
folder
Debug 文件夹
file
UDP通信程序.exe.manifest 4KB
file
GenerateResource-ResGen.write.1.tlog 698B
file
DesignTimeResolveAssemblyReferences.cache 789B
file
UDP通信程序.application 2KB
file
UDP通信程序.Properties.Resources.resources 180B
file
GenerateResource-ResGen.read.1.tlog 740B
file
DesignTimeResolveAssemblyReferencesInput.cache 6KB
file
UDP通信程序.pdb 24KB
file
UDP通信程序.exe 12KB
file
UDP通信程序.csproj.FileListAbsolute.txt 3KB
folder
TempPE 文件夹
file
Properties.Resources.Designer.cs.dll 5KB
file
UDP通信程序.UDPserverForm.resources 180B
file
UDP通信程序.csproj.GenerateResource.Cache 975B
folder
Properties 文件夹
file
Resources.Designer.cs 3KB
file
Settings.settings 249B
file
Resources.resx 5KB
file
Settings.Designer.cs 1KB
file
AssemblyInfo.cs 1KB
file
Program.cs 504B
file
Form1.Designer.cs 9KB
file
Form1.resx 6KB
file
UDP通信程序.csproj 6KB
file
app.config 117B
file
UDP通信程序.suo 20KB
file
UDP通信程序.sln 887B
rar 文件大小:56.47KB