有Main窗體,上面有個Button,當點擊Button時,有如下代碼:
Form1 frm = new Form1();
frm.ShowDialog();以下為Form1的部分代碼:
private void Form1_Load(object sender, EventArgs e)
{
GetUDPStatus();
}
struct UdpState
{
public System.Net.IPEndPoint EP;
public System.Net.Sockets.UdpClient UDPClient;
} private void GetUDPStatus()
{
GlobalUDP.UDPClient = new UdpClient();
GlobalUDP.EP = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 26868); IPEndPoint BindEP = new IPEndPoint(IPAddress.Any, 26868);
byte[] DiscoverMsg = Encoding.ASCII.GetBytes("Discovery:Who is out there?"); GlobalUDP.UDPClient.Client.Bind(BindEP);
GlobalUDP.UDPClient.MulticastLoopback = false;
GlobalUDP.UDPClient.EnableBroadcast = true; GlobalUDP.UDPClient.BeginReceive(ReceiveCallback, GlobalUDP);
GlobalUDP.UDPClient.Send(DiscoverMsg, DiscoverMsg.Length, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("255.255.255.255"), 26868));
} private void btnSearch_Click(object sender, EventArgs e)
{
SearchHost();
} private void SearchHost()
{
listView1.Items.Clear(); byte[] DiscoverMsg = Encoding.ASCII.GetBytes("Discovery: Who is out there?");
GlobalUDP.UDPClient.Send(DiscoverMsg, DiscoverMsg.Length, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("255.255.255.255"), 26868));
} public void AddDiscoveryEntry(object o)
{
listView1.Items.Add(new ListViewItem(((string)(o)).Split('\n')));
} public void ReceiveCallback(IAsyncResult ar)
{
UdpState MyUDP = (UdpState)ar.AsyncState; string ReceiveString = Encoding.ASCII.GetString(MyUDP.UDPClient.EndReceive(ar, ref MyUDP.EP)); //ReceiveString = MyUDP.EP.Address.ToString() + "\n" + ReceiveString.Replace("\r\n", "\n"); MyUDP.UDPClient.BeginReceive(ReceiveCallback, MyUDP); listView1.Invoke(new AddTolstDiscoveredDevices(AddDiscoveryEntry), ReceiveString);
}
第一次運行正確,當我關閉Form1後,再次點擊Button時,舊會出現如下錯誤:
Only one usage of each socket address (protocol/network address/port) is normally permitted
我猜測應該是關閉Form1窗體時沒有關閉異步和端口?有知道的幫忙看看!謝謝!
Form1 frm = new Form1();
frm.ShowDialog();以下為Form1的部分代碼:
private void Form1_Load(object sender, EventArgs e)
{
GetUDPStatus();
}
struct UdpState
{
public System.Net.IPEndPoint EP;
public System.Net.Sockets.UdpClient UDPClient;
} private void GetUDPStatus()
{
GlobalUDP.UDPClient = new UdpClient();
GlobalUDP.EP = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 26868); IPEndPoint BindEP = new IPEndPoint(IPAddress.Any, 26868);
byte[] DiscoverMsg = Encoding.ASCII.GetBytes("Discovery:Who is out there?"); GlobalUDP.UDPClient.Client.Bind(BindEP);
GlobalUDP.UDPClient.MulticastLoopback = false;
GlobalUDP.UDPClient.EnableBroadcast = true; GlobalUDP.UDPClient.BeginReceive(ReceiveCallback, GlobalUDP);
GlobalUDP.UDPClient.Send(DiscoverMsg, DiscoverMsg.Length, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("255.255.255.255"), 26868));
} private void btnSearch_Click(object sender, EventArgs e)
{
SearchHost();
} private void SearchHost()
{
listView1.Items.Clear(); byte[] DiscoverMsg = Encoding.ASCII.GetBytes("Discovery: Who is out there?");
GlobalUDP.UDPClient.Send(DiscoverMsg, DiscoverMsg.Length, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("255.255.255.255"), 26868));
} public void AddDiscoveryEntry(object o)
{
listView1.Items.Add(new ListViewItem(((string)(o)).Split('\n')));
} public void ReceiveCallback(IAsyncResult ar)
{
UdpState MyUDP = (UdpState)ar.AsyncState; string ReceiveString = Encoding.ASCII.GetString(MyUDP.UDPClient.EndReceive(ar, ref MyUDP.EP)); //ReceiveString = MyUDP.EP.Address.ToString() + "\n" + ReceiveString.Replace("\r\n", "\n"); MyUDP.UDPClient.BeginReceive(ReceiveCallback, MyUDP); listView1.Invoke(new AddTolstDiscoveredDevices(AddDiscoveryEntry), ReceiveString);
}
第一次運行正確,當我關閉Form1後,再次點擊Button時,舊會出現如下錯誤:
Only one usage of each socket address (protocol/network address/port) is normally permitted
我猜測應該是關閉Form1窗體時沒有關閉異步和端口?有知道的幫忙看看!謝謝!
解决方案 »
- 串口不返回值是为什么?
- c# word
- 菜鸟请教,如何在C#中设置窗体的最大化和最小化?
- 如何将一个页面中datagrid中的某一行记录的几个参数分别传送到另一个页面的text中?
- JustinIO这个类谁能看懂啊???我想请教一个清串口缓存的方法
- C#WINFROM开发中数据库问题
- 一个很怪异的问题,求高手相助!
- 求dataset的查询。。。。。。。。。急
- 作一个web项目,程序已经完成,可用户又提出新的需求,要和局域网邮件挂起来,请问高手有没有和exchang数据库相关的代码或组件?
- 不知如何是好。
- 对一个窗体类使用单例模式时, 再次调用出现"无法访问已释放的对象"错误提示!
- c# word 转tif
{
GlobalUDP.UDPClient.Close();
}
這樣的話,在 public void ReceiveCallback(IAsyncResult ar)裡又會產生錯誤!Cannot access a disposed object.
Object name: 'System.Net.Sockets.Socket'.這個函數是異步
{
GlobalUDP.UDPClient.Close(); }
试试这个
{
return;
}
sock.Blocking = false;
sock.Shutdown(SocketShutdown.Both);
sock.Close();
sock = null;
试试!!
string ReceiveString = Encoding.ASCII.GetString(MyUDP.UDPClient.EndReceive(ar, ref MyUDP.EP));Cannot access a disposed object.
Object name: 'System.Net.Sockets.UdpClient'.這行出錯了!
你解决了,可否给我解释一下,我的email:[email protected]