我自己创建了一个 名为UDP_Socket的控件,项目中多个Form都要使用该控件,该控件里有个UDPClient对象指向本机某个端口,每次调用初始化该控件,会出现端口重复使用异常!请高手帮忙解决.using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Windows.Forms;
using System.Threading;namespace QQ_Class
{
public partial class Udp_Socket : Component
{
private IPEndPoint ServerEndPoint = null;
private UdpClient UDP_Server = new UdpClient();
private System.Threading.Thread Thd_UDP; public delegate void DataArrivalEventHandler(byte[] Data, IPAddress ip, int port);
public event DataArrivalEventHandler DataArrival;
private string localHost = "127.0.0.1";
[Browsable(true), Category("Local"), Description("本地IP地址")] //在“属性”窗口中显示localHost属性
public string LocalHost
{
get { return localHost; }
set { localHost = value; }
}
private int localPort=1234;
[Browsable(true), Category("Local"), Description("本地端口号")] //在“属性”窗口中显示localPort属性
public int LocalPort
{
get { return localPort; }
set { localPort = value; }
} private bool active = false;
[Browsable(true), Category("Local"), Description("激活监听")] //在“属性”窗口中显示active属性 public bool Active
{
get { return active; }
set
{
active = value;
if (active)
{
OpenSocket();
}
else
{
CloseSocket();
}
}
}
public Udp_Socket()
{
InitializeComponent();
} public Udp_Socket(IContainer container)
{
container.Add(this);
InitializeComponent();
}
protected void Listener()
{
if (UDP_Server != null)
UDP_Server.Close(); ServerEndPoint = new IPEndPoint(IPAddress.Any, localPort); UDP_Server = new UdpClient(localPort, AddressFamily.InterNetwork); //////// 此处出现异常
try
{
Thd_UDP = new Thread(new ThreadStart(GetUDPData));
Thd_UDP.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
} private void GetUDPData()
{
while (active)
{
byte[] Data = UDP_Server.Receive(ref ServerEndPoint); if (DataArrival != null)
{
DataArrival(Data, ServerEndPoint.Address, ServerEndPoint.Port);
}
Thread.Sleep(0); }
} private void CallBackMethod(IAsyncResult ar)
{
DataArrivalEventHandler dh = (DataArrivalEventHandler)ar.AsyncState;
dh.EndInvoke(ar);
} public void Send(IPAddress host, int port, byte[] data)
{
IPEndPoint server = new IPEndPoint(host, port); UDP_Server.Send(data, data.Length, server);
} private void OpenSocket()
{
Listener();
} private void CloseSocket()
{
if (UDP_Server != null)
UDP_Server.Close();
if (Thd_UDP != null)
{
// Thread.Sleep(30);
// Thd_UDP.Abort();
Thd_UDP.Abort();
}
} }
}
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Windows.Forms;
using System.Threading;namespace QQ_Class
{
public partial class Udp_Socket : Component
{
private IPEndPoint ServerEndPoint = null;
private UdpClient UDP_Server = new UdpClient();
private System.Threading.Thread Thd_UDP; public delegate void DataArrivalEventHandler(byte[] Data, IPAddress ip, int port);
public event DataArrivalEventHandler DataArrival;
private string localHost = "127.0.0.1";
[Browsable(true), Category("Local"), Description("本地IP地址")] //在“属性”窗口中显示localHost属性
public string LocalHost
{
get { return localHost; }
set { localHost = value; }
}
private int localPort=1234;
[Browsable(true), Category("Local"), Description("本地端口号")] //在“属性”窗口中显示localPort属性
public int LocalPort
{
get { return localPort; }
set { localPort = value; }
} private bool active = false;
[Browsable(true), Category("Local"), Description("激活监听")] //在“属性”窗口中显示active属性 public bool Active
{
get { return active; }
set
{
active = value;
if (active)
{
OpenSocket();
}
else
{
CloseSocket();
}
}
}
public Udp_Socket()
{
InitializeComponent();
} public Udp_Socket(IContainer container)
{
container.Add(this);
InitializeComponent();
}
protected void Listener()
{
if (UDP_Server != null)
UDP_Server.Close(); ServerEndPoint = new IPEndPoint(IPAddress.Any, localPort); UDP_Server = new UdpClient(localPort, AddressFamily.InterNetwork); //////// 此处出现异常
try
{
Thd_UDP = new Thread(new ThreadStart(GetUDPData));
Thd_UDP.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
} private void GetUDPData()
{
while (active)
{
byte[] Data = UDP_Server.Receive(ref ServerEndPoint); if (DataArrival != null)
{
DataArrival(Data, ServerEndPoint.Address, ServerEndPoint.Port);
}
Thread.Sleep(0); }
} private void CallBackMethod(IAsyncResult ar)
{
DataArrivalEventHandler dh = (DataArrivalEventHandler)ar.AsyncState;
dh.EndInvoke(ar);
} public void Send(IPAddress host, int port, byte[] data)
{
IPEndPoint server = new IPEndPoint(host, port); UDP_Server.Send(data, data.Length, server);
} private void OpenSocket()
{
Listener();
} private void CloseSocket()
{
if (UDP_Server != null)
UDP_Server.Close();
if (Thd_UDP != null)
{
// Thread.Sleep(30);
// Thd_UDP.Abort();
Thd_UDP.Abort();
}
} }
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货