UDP Sockets in C#
UDP provides an end-to-end service different from that of TCP.
In fact, UDP performs only two functions:
(1) it adds another layer of addressing (ports) to that of IP
(2) it detects data corruption that may occur in transit and discards any corrupted messages
Diffenence from TCP Sockets:
1. UDP sockets do not have to be connected before being used.
2. TCP is like telephone, and UDP is like email.
UDP Client
The typical UDP client goes through three steps:
1. Construct an instance of UdpClient, optionally specifying the local address and port.
2. Communicate by sending and receiving datagrams (byte arrays) using the Send() and
  Receive() methods of UdpClient.
3. When finished, deallocate the socket using the Close() method of UdpClient.
UdpEchoClient.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zeus.Thunder.Core;
using System.Net; // For IPEndPoint
using System.Net.Sockets; // For UdpClient, SocketException namespace SharpTrainer.NetWork
{
class UdpEchoClient : ITestCase
{
public void Run()
{
Console.Write("Input Server IP: ");
string server = Console.ReadLine(); Console.Write("Input Server Port: ");
int servPort = Int32.Parse(Console.ReadLine()); Console.Write("Input Echo String: ");
byte[] sendPacket = Encoding.ASCII.GetBytes(Console.ReadLine()); // Create a UdpClient instance
UdpClient client = new UdpClient();
try
{
// Send the echo string to the specified host and port
client.Send(sendPacket, sendPacket.Length, server, servPort);
Console.WriteLine("Sent {0} bytes to the server...", sendPacket.Length); // This IPEndPoint instance will be populated with the remote sender’s
// endpoint information after the Receive() call
IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, ); // Attempt echo reply receive
byte[] rcvPacket = client.Receive(ref remoteIPEndPoint); Console.WriteLine("Received {0} bytes from {1}: {2}", rcvPacket.Length, remoteIPEndPoint,
Encoding.ASCII.GetString(rcvPacket, , rcvPacket.Length));
}
catch (SocketException se)
{
Console.WriteLine(se.ErrorCode + ": " + se.Message);
} client.Close();
}
}
}
UdpEchoServer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zeus.Thunder.Core;
using System.Net; // For IPEndPoint
using System.Net.Sockets; // For UdpClient, SocketException namespace SharpTrainer.NetWork
{
class UdpEchoServer : ITestCase
{
public void Run()
{
Console.Write("Input Server Port: ");
int servPort = Int32.Parse(Console.ReadLine()); UdpClient client = null;
try {
// Create an instance of UdpClient on the port to listen on
client = new UdpClient(servPort);
} catch (SocketException se) {
Console.WriteLine(se.ErrorCode + ": " + se.Message);
Environment.Exit(se.ErrorCode);
} // Create an IPEndPoint instance that will be passed as a reference
// to the Receive() call and be populated with the remote client info
IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, ); for (;;)
{
// Run forever, receiving and echoing datagrams
try {
// Receive a byte array with echo datagram packet contents
byte[] byteBuffer = client.Receive(ref remoteIPEndPoint);
Console.Write("Handling client at " + remoteIPEndPoint + " - ");
// Send an echo packet back to the client
client.Send(byteBuffer, byteBuffer.Length, remoteIPEndPoint);
Console.WriteLine("echoed {0} bytes.", byteBuffer.Length);
} catch (SocketException se) {
Console.WriteLine(se.ErrorCode + ": " + se.Message);
}
}
}
}
}
运行结果:
Server端:
Handling client at 127.0.0.1:65005 - echoed 18 bytes.
Client端:
Input Server IP: 127.0.0.1
Input Server Port: 9999
Input Echo String: Hello Master HaKu!
Sent 18 bytes to the server...
Received 18 bytes from 127.0.0.1:9999: Hello Master HaKu!
UDP Sockets in C#的更多相关文章
- Windows UDP sockets: recvfrom() fails with error 10054
		
https://stackoverflow.com/questions/34242622/windows-udp-sockets-recvfrom-fails-with-error-10054 #in ...
 - 【Network】高性能 UDP 应该怎么做?
		
参考资料: EPOLL-UDP-GOLANG golang udp epoll - Google 搜索 go - golang: working with multiple client/server ...
 - 开源免费的C/C++网络库(c/c++ sockets library)
		
(1)ACE 庞大.复杂,适合大型项目.开源.免费,不依赖第三方库,支持跨平台. http://www.cs.wustl.edu/~schmidt/ACE.html (2)Asio Asio基于Boo ...
 - 开源免费的C/C++网络库(c/c++ sockets library)补充
		
(1)ACE 庞大.复杂,适合大型项目.开源.免费,不依赖第三方库,支持跨平台. http://www.cs.wustl.edu/~schmidt/ACE.html (2)Asio Asio基于Boo ...
 - 【RL-TCPnet网络教程】第17章 RL-TCPnet之UDP通信
		
第17章 RL-TCPnet之UDP通信 本章节为大家讲解RL-TCPnet的UDP通信实现,学习本章节前,务必要优先学习第16章UDP用户数据报协议基础知识.有了这些基础知识之后,再搞本章 ...
 - ss is one another utility to investigate sockets(特适合大规模tcp链接)
		
原创文章,转载请注明: 转载自系统技术非业余研究 本文链接地址: ss is one another utility to investigate sockets(特适合大规模tcp链接) 具体的可以 ...
 - 开源免费的C/C++网络库(c/c++ sockets library)(转)
		
原文转自 http://blog.csdn.net/weiwangchao_/article/details/8730199 (1)ACE 庞大.复杂,适合大型项目.开源.免费,不依赖第三方库,支持跨 ...
 - TCP/UDP server
		
Simple: Sample TCP/UDP server https://msdn.microsoft.com/en-us/library/aa231754(v=vs.60).aspx Simple ...
 - fuser - identify processes using files or sockets
		
FUSER(1) User Commands FUSER(1) NAME fuser - identify processes using files or sockets SYNOPSIS fuse ...
 
随机推荐
- java面试题一
			
个人的一点参考总结,如有雷同,纯属巧合! 1.hashmap的实现原理以及hashtable的线程安全是怎么实现的?HashMap其实也是一个线性的数组实现的,所以可以理解为其存储数据的容器就是一个线 ...
 - FastReport.Net使用:[37]报表继承
			
1.设计一个基础报表,将其保存为BaseReport. 2.新建一个继承的报表. 通过 文件-->新建 打开“新建对象”向导.选择“继承的报表”,点击确定. 3. 在打开对话框中选择基础报表Ba ...
 - Codeforces 388 D. Fox and Perfect Sets
			
$ >Codeforces \space 388 D. Fox and Perfect Sets<$ 题目大意 : 定义一个完美的集合 \(S\) ,当且仅当 \(S\) 非负非空,且 ...
 - 【最大独立集】BZOJ3175- [Tjoi2013]攻击装置
			
[题目大意] 给定一个01矩阵,其中你可以在0的位置放置攻击装置.每一个攻击装置(x,y)都可以按照“日”字攻击其周围的 8个位置(x-1,y-2),(x-2,y-1),(x+1,y-2),(x+2, ...
 - 【LCA/tarjan】POJ1470-Closest Common Ancestors
			
[题意] 给出一棵树和多组查询,求以每个节点为LCA的查询数有多少? [错误点] ①读入的时候,注意它的空格是随意的呀!一开始不知道怎么弄,后来看了DISCUSS区大神的话: 询问部分输入: scan ...
 - [HDU1290]献给杭电五十周年校庆的礼物
			
[HDU1290]献给杭电五十周年校庆的礼物 题目大意: 问\(n(n\le1000)\)个平面能够将一个三维空间分成几部分. 思路: 公式\(\frac{n^3+5n+6}6\). 源代码: #in ...
 - Matlab 曲线绘制之线型和颜色 示例
			
估计很多人会碰到,当绘制的曲线特别多的时候,需要用不同的颜色和线型区分开这些曲线.根据STC论文,自己整理了一个颜色和线型的例子,供大家直接使用,直接引用PlotStyle这个数据结构就可以了. 示例 ...
 - 感觉比较好一些的vultr中文网
			
搜索一波vultr的中文网,发现很多,基本上都是靠活动以及一些SS的教程维持,全都是大同小异,感觉比较有可比性估计一下这个: https://www.thevultr.org/ 可以对比一些美国常用V ...
 - Spring EL运算符实例
			
Spring EL支持大多数标准的数学,逻辑和关系运算符. 例如, 关系运算符 – 等于 (==, eq), 不等于 (!=, ne), 小于 (<, lt), 小于或等于 (<= , l ...
 - matlab colormap
			
This table lists the built-in colormaps functions. Colormap Name Color Scale parula