UDP通信类,监听本机指定端口,发送消息给指定电脑
来源:互联网 发布:windows nt 4.0 sp6 编辑:程序博客网 时间:2024/05/18 04:00
using System;
using System.Collections.Generic;
using System.Text;
//添加的命名空间引用
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text.RegularExpressions;
namespace CoreMes.UDP
{
class UDP
{
int remotePort;
string remoteIp;
int ReceviePort;
IPEndPoint remotePoint;
public delegate void ReceiverDataDel(string message);
public event ReceiverDataDel ReceiverdataEvent;
/// <summary>
/// 初始化
/// </summary>
/// <param name="remoteip">远程主机ip</param>
/// <param name="remoteP">远程主机端口</param>
/// <param name="port">本地主机端口</param>
public UDP(string remoteip,int remoteP,int port)
{
this.remotePort = remoteP;
this.ReceviePort = port;
Regex ipRe = new Regex(@"2[0-4]\d|25[0-5]|1\d?\d?|[2-99]\.(2[0-4]\d|25[0-5]|0|1\d?\d?|[2-99]\.){2}2[0-4]\d|25[0-5]|0|1\d?\d?|[2-99]");
if(ipRe.IsMatch(remoteip))
{
this.remoteIp = remoteip;
remotePoint = new IPEndPoint(IPAddress.Parse(remoteIp), this.remotePort);
}
else
{
throw new Exception("ip地址格式不对");
}
//本地监听端口
Thread receiverThread = new Thread(this.ReceiveData);
receiverThread.IsBackground = true;
receiverThread.Start();
}
/// <summary>
/// 在后台运行的接收线程
/// </summary>
private void ReceiveData()
{
//在本机指定的端口接收
UdpClient udpClient = new UdpClient(ReceviePort);
IPEndPoint remote = null;
//接收从远程主机发送过来的信息;
while (true)
{
try
{
//关闭udpClient时此句会产生异常
byte[] bytes = udpClient.Receive(ref remote);
string str = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
//消息处理
if(ReceiverdataEvent!=null)
{
ReceiverdataEvent(remote.ToString()+":"+str);
}
}
catch(Exception ex)
{
throw ex;
}
}
}
/// <summary>
/// 发送数据到远程主机
/// </summary>
public void SendData(string message)
{
UdpClient myUdpClient = new UdpClient();
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(message);
try
{
myUdpClient.Send(bytes, bytes.Length, remotePoint);
myUdpClient.Close();
}
catch (Exception err)
{
throw err;
}
finally
{
myUdpClient.Close();
}
}
}
}
using System.Collections.Generic;
using System.Text;
//添加的命名空间引用
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text.RegularExpressions;
namespace CoreMes.UDP
{
class UDP
{
int remotePort;
string remoteIp;
int ReceviePort;
IPEndPoint remotePoint;
public delegate void ReceiverDataDel(string message);
public event ReceiverDataDel ReceiverdataEvent;
/// <summary>
/// 初始化
/// </summary>
/// <param name="remoteip">远程主机ip</param>
/// <param name="remoteP">远程主机端口</param>
/// <param name="port">本地主机端口</param>
public UDP(string remoteip,int remoteP,int port)
{
this.remotePort = remoteP;
this.ReceviePort = port;
Regex ipRe = new Regex(@"2[0-4]\d|25[0-5]|1\d?\d?|[2-99]\.(2[0-4]\d|25[0-5]|0|1\d?\d?|[2-99]\.){2}2[0-4]\d|25[0-5]|0|1\d?\d?|[2-99]");
if(ipRe.IsMatch(remoteip))
{
this.remoteIp = remoteip;
remotePoint = new IPEndPoint(IPAddress.Parse(remoteIp), this.remotePort);
}
else
{
throw new Exception("ip地址格式不对");
}
//本地监听端口
Thread receiverThread = new Thread(this.ReceiveData);
receiverThread.IsBackground = true;
receiverThread.Start();
}
/// <summary>
/// 在后台运行的接收线程
/// </summary>
private void ReceiveData()
{
//在本机指定的端口接收
UdpClient udpClient = new UdpClient(ReceviePort);
IPEndPoint remote = null;
//接收从远程主机发送过来的信息;
while (true)
{
try
{
//关闭udpClient时此句会产生异常
byte[] bytes = udpClient.Receive(ref remote);
string str = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
//消息处理
if(ReceiverdataEvent!=null)
{
ReceiverdataEvent(remote.ToString()+":"+str);
}
}
catch(Exception ex)
{
throw ex;
}
}
}
/// <summary>
/// 发送数据到远程主机
/// </summary>
public void SendData(string message)
{
UdpClient myUdpClient = new UdpClient();
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(message);
try
{
myUdpClient.Send(bytes, bytes.Length, remotePoint);
myUdpClient.Close();
}
catch (Exception err)
{
throw err;
}
finally
{
myUdpClient.Close();
}
}
}
}
阅读全文
0 0
- UDP通信类,监听本机指定端口,发送消息给指定电脑
- UDP通信类,监听本机指定端口,发送消息给指定电脑
- UDP指定客户端发送端口
- RabbitMQ消息通信,生产者发送消息给指定的消费者的消息队列
- webservice向指定socket端口发送消息
- C# 给指定应用程序发送消息
- C# 给指定应用程序发送消息
- 【极光推送】给指定用户发送消息
- Java 通过Socket监听指定服务器(IP)的指定端口,及向指定服务器的指定端口发送信息
- Socket UDP通信小结(指定客户端的接受端口)
- ActiveMQ 发送指定消息
- 聊天自动发送消息给指定的人或群
- 让adbd监听指定端口
- UDP通信绑定指定IP
- 向指定窗口发送消息
- LWIP UDP socket编程 可以指定本地端口号及发送长度不能太长问题分析
- log4j.properties配置向指定ip的端口发送日志(udp)
- LWIP UDP socket编程 可以指定本地端口号及发送长度不能太长问题分析
- 面向对象技术
- 【Java面试】---装箱拆箱问题汇总
- python-II
- Android 通讯录(2)-----自定义View实现右侧导航栏
- HDU1754[I HATE IT] 线段树
- UDP通信类,监听本机指定端口,发送消息给指定电脑
- 练习赛zi树上距离(分治)
- 通过反射来创建对象,newInstance()和getConstructor()
- WebApi 断点上传
- linux中关闭正在运行的脚本
- IIPP Week 3
- 【容斥原理】
- JavaBeen类的几种写法及优缺点
- vue.js实例todoList