|
本帖最后由 头文字C 于 2013-10-5 22:31 编辑
为什么我调用Socket.Dispose()函数后程序会进入BeginAccept的回调函数里?
我的程序如下希望各位能帮我看下。感激不禁!- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading;
- //using System.Windows.Forms;
- namespace TCP
- {
- delegate void dgSetText(string str);//第一步:委托声明
- delegate int dgGetPort();//第一步:委托声明
-
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- static bool bConnecting = true;
- static bool bDisconnect = false;
- Thread gMyThread;
- private static bool vIsConnect = bDisconnect;
- static Socket gNewSocket;
- static Socket gClient;
- static byte[] gReBuf = new byte[1024];
- public MainWindow()
- {
- InitializeComponent();
- }
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- IPHostEntry Here = Dns.GetHostEntry(Dns.GetHostName());//Dns.GetHostName()获取本地主机名
- IPAddress LocalAddress = Here.AddressList[3];//需要完善
- tbServerAdd.Text = LocalAddress.ToString();
-
-
- }
- //服务器侦听等待线程
- private void thfAcceptConnection()
- {
- //try
- {
- int Port;
- dgGetPort gt = new dgGetPort(dgfGetPort);//第二步:实例化委托
- Port = (int)this.Dispatcher.Invoke(gt);
- IPHostEntry here = Dns.GetHostEntry(Dns.GetHostName());//Dns.GetHostName()获取本地主机名
- IPAddress localaddress = here.AddressList[3];//
- IPEndPoint iep = new IPEndPoint(localaddress, Port);
- gNewSocket = new Socket(AddressFamily.InterNetwork,
- SocketType.Stream,
- ProtocolType.Tcp);
- gNewSocket.Bind(iep);
- gNewSocket.Listen(20);
- while (vIsConnect == bDisconnect)
- {
- gNewSocket.BeginAccept(new AsyncCallback(cbfAccpet), gNewSocket);
- Thread.Sleep(1);
- }
- }
- //catch (Exception thfA_error)
- //{
- // MessageBox.Show(thfA_error.Message.ToString(), "thfA_exception");
- //}
- }
- //服务器接受Accept回调函数
- private void cbfAccpet(IAsyncResult iar)//调用gNewSocket.Dispose();后进入这里
- {
-
- if (vIsConnect == bDisconnect)
- {
- //try
- {
- Socket oldserver = (Socket)iar.AsyncState;
- gClient = oldserver.EndAccept(iar);
- gClient.BeginReceive(gReBuf ,0,gReBuf .Length,
- SocketFlags.None, new AsyncCallback(cbfRead), gClient);//*******
- if (gClient.Connected == true)
- {
- vIsConnect = bConnecting;
- }
- }
- //catch (Exception cbfA_error)
- //{
- // MessageBox.Show(cbfA_error.Message.ToString(), "cbfA_exception");
- //}
- }
- }
- //异步接收数据回调函数
- private void cbfRead(IAsyncResult iar)
- {
- try
- {
- if (vIsConnect == bConnecting)//
- {
- Socket so = (Socket)iar.AsyncState;
- int recv = so.EndReceive(iar);
- string str = Encoding.ASCII.GetString(gReBuf , 0, recv);
- IPEndPoint client = (IPEndPoint)so.RemoteEndPoint;
- dgSetText ST = new dgSetText(dgfShowText);//第二步:实例化委托
- string dgfShowTextshow = client.Address.ToString() + "\t" + str + "。";
- //this.Dispatcher.Invoke(ST, client.Address.ToString());
- this.Dispatcher.Invoke(ST,dgfShowTextshow);
- //显示完成后继续执行read回调函数接受数据
- so.BeginReceive(gReBuf , 0, gReBuf .Length,
- SocketFlags.None, new AsyncCallback(cbfRead), so);
- }
- }
- catch (Exception cbfR_error)
- {
- MessageBox.Show(cbfR_error.Message.ToString(), "cbfR_exception");
- }
- }
- //异步发送回调函数
- private void cbfSend(IAsyncResult iar)
- {
- try
- {
- gClient.EndSend(iar);
- }
- catch (Exception cbfs_error)
- {
- MessageBox.Show(cbfs_error.Message.ToString(), "cbfs_exception");
- }
- }
- private void dgfShowText(string str)//第0步:定义方法
- {
- receiveBox1.AppendText(str + "\r");//显示文本内容
- receiveBox1.ScrollToEnd();//将控件的内容滚动到当前插入符号位置。
- }
- private int dgfGetPort()//第0步:定义方法
- {
- int Port;
- Port = Convert.ToInt32(PortNum.Text);
- return Port;
- }
- //侦听按钮回调函数
- private void btListen_Click(object sender, RoutedEventArgs e)
- {
- btListen.IsEnabled = false;
- btListen.Foreground = Brushes.Gray;//按钮点击后失能并变灰
- gMyThread=new Thread(new ThreadStart(thfAcceptConnection));
- gMyThread.Start();
- }
-
- private void btClose_Click(object sender, RoutedEventArgs e)
- {
- //try
- {
- vIsConnect = bDisconnect;//
- btListen.IsEnabled = true;
- btListen.Foreground = Brushes.Black;
- gClient.Shutdown(SocketShutdown.Both);//禁用某Socket上的发送和接收。
- gClient.Close();
- gNewSocket.Dispose();//调用后进入cbfAccept
- gMyThread.Abort();
-
- }
- //catch (Exception btC_error)
- //{
- // MessageBox.Show(btC_error.Message.ToString(), "btC_exception");
- //}
-
- }
- private void btSend_Click(object sender, RoutedEventArgs e)
- {
-
- //try
- {
- byte[] sdata = Encoding.ASCII.GetBytes(tbSendStr.Text + "\r");
- gClient.BeginSend(sdata, 0, sdata.Length, SocketFlags.None,
- new AsyncCallback(cbfSend), gClient);
- }
- //catch (Exception bts_error)
- //{
- // MessageBox.Show(bts_error.Message.ToString(), "bts_exception");
- //}
- }
- private void Window_Closed(object sender, EventArgs e)
- {
- gClient.Shutdown(SocketShutdown.Both);//禁用某Socket上的发送和接收。
- gClient.Close();
- gNewSocket.Dispose();
- gMyThread.Abort();
-
- }
- }
- }
复制代码 |
|