您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

关于C#如何实现的UDP收发请求工具类的示例代码分析

2025/3/10 13:47:12发布20次查看
这篇文章主要介绍了c#实现的udp收发请求工具类,结合具体实例形式分析了c#针对udp请求的监听、接收、发送等相关操作技巧,需要的朋友可以参考下
本文实例讲述了c#实现的udp收发请求工具类。分享给大家供大家参考,具体如下:
初始化:
listeningport = int.parse(configurationmanager.appsettings["listeningport"]); sendingport = int.parse(configurationmanager.appsettings["sendingport"]); sendingip = configurationmanager.appsettings["sendingip"];
监听:
public static void listen() { task.run(() => { var done = false; var listener = new udpclient(listeningport); var groupep = new ipendpoint(ipaddress.any, listeningport); string received_data; byte[] receive_byte_array; try { _log.error("############service started###########"); while (true) { receive_byte_array = listener.receive(ref groupep); console.writeline("received a broadcast from {0}", groupep.tostring()); received_data = encoding.utf8.getstring(receive_byte_array, 0, receive_byte_array.length); parsecommand(received_data); } } catch (exception e) { _log.error(e); console.writeline(e.tostring()); } _log.error("############service stopped###########"); }); }
发送:
public static void sendcommand(string xmlcmd) { try { var sending_socket = new socket(addressfamily.internetwork, sockettype.dgram, protocoltype.udp); var sending_end_point = new ipendpoint(ipaddress.parse(sendingip), sendingport); var send_buffer = encoding.utf8.getbytes(xmlcmd); sending_socket.sendto(send_buffer, sending_end_point); _log.info("[command sent] : " + xmlcmd); } catch (exception ex) { _log.error(ex); } }
以上就是关于c#如何实现的udp收发请求工具类的示例代码分析的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product