我自己写了一个tcp收发数据的dll文件,不过每次关闭的时候都跳到红色的那本分报错请问怎么回事。
我在程序关闭的时候是调用了
ActiveSocket =false; //去关闭端口
this.close();(this是我的一个窗体,并且创建了下面dll的文件中的类对象)
然后就跳到代码中红色的那部分去了。
请问我的dll代码哪有问题。我感觉就是循环那部分有问题,但是又找不出来问题
using System;
using System.IO;
using System.Text;
using System.Net;
using System.Data;
using System.Threading;
using System.Net.Sockets;
using System.Collections.Generic;
namespace TcpClassLb
{
    public class SocketDataClass
    {
        public delegate void DataArrivalEvntHandle(string sockData, TcpClient curClient);   //数据到达事件代理
        public delegate void DataErrorEvntHandle(object sender, Exception e);                       //错误事件代理        private Thread myTcpThread = null;
        private IPEndPoint curipend;                         //当前连接的节点地址
        private TcpListener myTcpListener = null;        public event DataArrivalEvntHandle dataArrivalEvnt;
        public event DataErrorEvntHandle dataErrorEvnet;        private int m_LocalListenPort = 6800;
        public int LocalListenPort
        {
            get { return m_LocalListenPort; }
            set { m_LocalListenPort = value; }
        }        private bool m_ActiveSocket = false;  //当前连接是否需要激活
        public bool ActiveSocket
        {
            get { return m_ActiveSocket; }            set
            {
                m_ActiveSocket = value;                if (m_ActiveSocket) OpenTcpSocket();
                else CloseTcpSocket();
            }
        }        public void OpenTcpSocket()  //开始监听
        {
            myTcpThread = new Thread(new ThreadStart(ListenerProc));
            myTcpThread.Start();
        }        public bool CloseTcpSocket() //关闭监听
        {
            if (myTcpListener != null)
            {
                myTcpListener.Stop();            }            if (myTcpThread != null)
            {
                myTcpThread.Abort();
                myTcpThread.Join(100);
                //Thread.CurrentThread.Abort();
            }
            return true;
        }        public void ListenerProc()
        {
            try
            {
                NetworkStream netStream;                myTcpListener = new TcpListener(m_LocalListenPort);
                myTcpListener.Start();                while (true)
                {
                    TcpClient ListenedClient = myTcpListener.AcceptTcpClient();
                    Socket sock = ListenedClient.Client;
                    curipend = (IPEndPoint)sock.RemoteEndPoint;
                    netStream = ListenedClient.GetStream();
                    DataRecvSeriaze(netStream, ListenedClient);                    netStream.Close();                   // ListenedClient.Client.Shutdown(SocketShutdown.Both);
                    ListenedClient.Close();                    Thread.Sleep(0);
                }
            }            catch (Exception e)
            {
                dataErrorEvnet(this, e);
            }
        }        ///数据接收
        private void DataRecvSeriaze(NetworkStream NetStream, TcpClient curClient)
        {
            string revData;
            //byte[] data = new byte[4];
            byte[] recv = new byte[2048];
            int size = NetStream.Read(recv, 0, 2048);            if (size > 0)
            {                revData = System.Text.Encoding.UTF8.GetString(recv, 0, size);
                dataArrivalEvnt(revData, curClient);
            }
        }        /// <summary>
        /// 数据发送 
        /// </summary>
        /// <param name="curClient"></param>
        /// <param name="datasend"></param>        public void DataSendSeriaze(TcpClient curClient, string datasend)
        {
            if (datasend.Length > 0)
            {
                try
                {
                    lock (curClient.GetStream())
                    {
                        byte[] send = new byte[datasend.Length];
                        send = System.Text.Encoding.UTF8.GetBytes(datasend);
                        int memsize = (int)datasend.Length;                        curClient.GetStream().Write(send, 0, memsize);
                        curClient.GetStream().Flush();
                    }                    curClient.GetStream().Close();                    curClient.Close();
                }                catch (Exception e)
                {
                    curClient.GetStream().Close();
                    curClient.Close();
                    dataErrorEvnet(this, e);
                }
            }
        }
    }
}