UDP通信类优化版
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
{
public class ReceiverMessageEventArgs
{
public IPEndPoint EndPoint;
public string Messaage;
}
public class UDP
{
Thread receiverThread;
int remotePort;
string remoteIp;
int receviePort;
Boolean isListener = false;
IPEndPoint remotePoint;
public delegate void ReceiverDataDel(ReceiverMessageEventArgs e);
public event ReceiverDataDel ReceiverdataEvent;
#region 属性
/// <summary>
/// 远程主机端口
/// </summary>
public int RemotePort
{
get { return remotePort; }
set { remotePort = value; }
}
/// <summary>
/// 远程主机IP
/// </summary>
public string RemoteIp
{
get { return remoteIp; }
set { remoteIp = value; }
}
/// <summary>
/// 本地接收信息端口
/// </summary>
public int ReceviePort
{
get { return receviePort; }
set { receviePort = value; }
}
/// <summary>
/// 设置是否监本地端口消息
/// </summary>
public Boolean IsListener
{
get { return isListener; }
set
{
if (isListener != value)
{
isListener = value;
if (value)
{
receiverThread.IsBackground = true;
receiverThread.Start();
}
}
}
}
/// <summary>
/// 对方主机的EndPoint
/// </summary>
public IPEndPoint RemotePoint
{
get { return remotePoint; }
set { remotePoint = value; }
}
#endregion
#region 构造函数
public UDP()
{
receiverThread = new Thread(ReceiveData);
}
/// <summary>
/// 初始化
/// </summary>
/// <param name="remoteip">远程主机ip</param>
/// <param name="remoteP">远程主机端口</param>
/// <param name="port">本地主机端口</param>
public UDP(string remoteip, int remoteP, int port, bool isListen)
{
receiverThread = new Thread(ReceiveData);
this.remotePort = remoteP;
this.receviePort = port;
this.isListener = isListen;
Regex ipRe = new Regex(@"2\d|25|1\d?\d?|\.(2\d|25|0|1\d?\d?|\.){2}2\d|25|0|1\d?\d?|");
if (ipRe.IsMatch(remoteip))
{
this.remoteIp = remoteip;
remotePoint = new IPEndPoint(IPAddress.Parse(remoteIp), this.remotePort);
}
else
{
throw new Exception("ip地址格式不对");
}
//是否自动监听
if (isListener)
{
Listener();
}
}
/// <summary>
/// 只监听本地端口
/// </summary>
/// <param name="port">端口</param>
/// <param name="isListen">是否开启监听</param>
public UDP(int port, bool isListen)
{
receiverThread = new Thread(ReceiveData);
this.receviePort = port;
this.isListener = isListen;
//是否自动监听
if (isListener)
{
Listener();
}
}
/// <summary>
/// 配置远程电脑ip和端口
/// </summary>
/// <param name="remoteip">ip</param>
/// <param name="remoteP">端口</param>
public UDP(string remoteip, int remoteP)
{
receiverThread = new Thread(ReceiveData);
this.remotePort = remoteP;
Regex ipRe = new Regex(@"2\d|25|1\d?\d?|\.(2\d|25|0|1\d?\d?|\.){2}2\d|25|0|1\d?\d?|");
if (ipRe.IsMatch(remoteip))
{
this.remoteIp = remoteip;
remotePoint = new IPEndPoint(IPAddress.Parse(remoteIp), this.remotePort);
}
else
{
throw new Exception("ip地址格式不对");
}
}
#endregion
publicvoid Listener()
{
isListener = true;
//本地监听端口
receiverThread.IsBackground = true;
receiverThread.Start();
}
/// <summary>
/// 在后台运行的接收线程
/// </summary>
privatevoid ReceiveData()
{
//在本机指定的端口接收
if (receviePort == 0) //监听端口不能为0,不赋值
throw new Exception("监听端口不能为空");
UdpClient udpClient=null;
try
{
udpClient = new UdpClient(receviePort);
}
catch
{
System.Windows.Forms.MessageBox.Show("端口"+receviePort+"已被占用!","提示");
this.IsListener=false;
return;
}
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)
{
ReceiverMessageEventArgs e = new ReceiverMessageEventArgs();
e.EndPoint = remote;
e.Messaage = str;
ReceiverdataEvent(e);
}
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message, "提示");
this.IsListener = false;
return;
}
}
}
/// <summary>
/// 发送数据到远程主机
/// </summary>
public void SendData(string message)
{
if (remotePoint == null)
{
if (remoteIp != null && remoteIp != "" && remotePort != 0)
{
remotePoint = new IPEndPoint(IPAddress.Parse(remoteIp), remotePort);
}
else
{
throw new Exception("没有设置对方主机的ip和端口");
}
}
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();
}
}
}
}
页:
[1]
2