c# socket编程udp客户端实现代码分享
时间:2023-06-16 05:03:31
Console.WriteLine("This is a Client, host name is {0}", Dns.GetHostName());
//设置服务端终结点
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8001);
//创建与服务端连接的套接字,指定网络类型,数据连接类型和网络协议
Socket ConnSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
string welcome = "Client Message:Hello!!!";
byte[] data = new byte[1024];
data = Encoding.ASCII.GetBytes(welcome);
//给服务端发送测试消息
ConnSocket.SendTo(data, data.Length, SocketFlags.None, ipe);
IPEndPoint server = new IPEndPoint(IPAddress.Any, 0);
//服务端终结点
EndPoint Remote = (EndPoint)server;
data = new byte[1024];
//对于不存在的IP地址,加入此行代码后,可以在指定时间内解除阻塞模式限制
//server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 100);
int recv = ConnSocket.ReceiveFrom(data, ref Remote);
//打印从服务端发回的信息
Console.WriteLine("Message received from {0}: ", Remote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
while (true) //可以实时给服务端发送消息
{
string input = Console.ReadLine();
if (input == "exit") //中断连接
{
ConnSocket.SendTo(Encoding.ASCII.GetBytes(input), Remote);
data = new byte[1024];
recv = ConnSocket.ReceiveFrom(data, ref Remote);
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
break;
}
else
{
ConnSocket.SendTo(Encoding.ASCII.GetBytes("Client Message:" + input), Remote);
data = new byte[1024];
recv = ConnSocket.ReceiveFrom(data, ref Remote);
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
}
}
Console.WriteLine("Stopping Client.");
ConnSocket.Close();
标签:socket编程udp,udp客户端
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java简单实现斗地主发牌功能
2023-06-18 16:22:44
![](https://img.aspxhome.com/file/2023/6/57506_0s.jpg)
Flutter模仿实现微信底部导航栏流程详解
2023-06-21 11:46:12
![](https://img.aspxhome.com/file/2023/3/100603_0s.png)
2020.2 IntelliJ IDEA激活与IDEA2020.2破解详细教程
2023-06-20 11:36:12
![](https://img.aspxhome.com/file/2023/4/57414_0s.jpg)
JSON.toJSONString()空字段不忽略修改的问题
2023-06-16 03:12:37
普通对象使用spring容器中的对象的实现方法
2023-06-17 12:27:20
C++实现LeetCode(9.验证回文数字)
2023-06-21 00:20:38
在Flutter中制作翻转卡片动画的完整实例代码
2023-06-23 23:31:21
flutter material widget组件之信息展示组件使用详解
2023-06-22 08:45:35
springsecurity 企业微信登入的实现示例
2023-06-16 16:39:35
![](https://img.aspxhome.com/file/2023/0/57490_0s.jpg)
java使double保留两位小数的多方法 java保留两位小数
2023-06-17 14:01:30
详解基于spring多数据源动态调用及其事务处理
2023-06-23 14:37:25
![](https://img.aspxhome.com/file/2023/0/57530_0s.png)
c# Selenium爬取数据时防止webdriver封爬虫的方法
2023-06-24 07:50:51
![](https://img.aspxhome.com/file/2023/7/59117_0s.png)
C语言预处理预编译命令及宏定义详解
2023-06-18 16:28:06
![](https://img.aspxhome.com/file/2023/3/83503_0s.png)
Java springboot yaml语法注解
2023-06-17 08:13:35
flutter实现appbar下选项卡切换
2023-06-21 13:35:24
![](https://img.aspxhome.com/file/2023/3/87313_0s.gif)
iOS WebView中使用webp格式图片的方法
2023-06-17 22:06:03
C++实现LeetCode(205.同构字符串)
2023-06-21 04:06:54
C++实现LeetCode(159.最多有两个不同字符的最长子串)
2023-06-20 22:39:46
10种简单的Java性能优化
2023-06-20 20:43:41
![](https://img.aspxhome.com/file/2023/6/57526_0s.png)
Flutter路由传递参数及解析实现
2023-06-22 11:48:45
![](https://img.aspxhome.com/file/2023/5/90155_0s.gif)