static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);            TCP t = new TCP();
            t.fnListen();        }
    }    public class TCP
    {
        private Socket host = null;        private Socket soc = null;        public TCP()
        {
            this.host = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress ip = Dns.GetHostAddresses(Dns.GetHostName())[0];
            IPEndPoint ep = new IPEndPoint(ip, 19080);
            host.Bind(ep);
            this.fnListen();
        }        public void fnListen()
        {
            int i = 0;
            host.Listen(20);
            while (true)
            {                soc = host.Accept();
                i += 1;
                System.Threading.ThreadStart ts = new System.Threading.ThreadStart(fnREV);
                System.Threading.Thread th = new System.Threading.Thread(ts);
                th.Name = "thread name =" + i.ToString();
                Console.WriteLine(th.Name);
                th.Start();
            }
        }        public void fnREV()
        {
            TCPone one = new TCPone(this.soc);
            one.fnStart();
        }
    }    public class TCPone
    {
        Socket host = null;
        public TCPone(Socket soc)
        {
            host = soc;
        }        public void fnStart()
        {
            while (true)
            {
                try
                {
                    byte[] byt = new byte[50];
                    this.host.Receive(byt);
                    string temp = System.Text.Encoding.Default.GetString(byt);
                    Console.WriteLine(temp + "  thread name " + System.Threading.Thread.CurrentThread.Name);
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }            }            Console.WriteLine("  thread name " + System.Threading.Thread.CurrentThread.Name);
        }    }    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);            TcpClient client = new TcpClient();
            client.fnMain();        }
    }    public class TcpClient
    {
        public TcpClient()
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine(i);
                System.Threading.ThreadStart ts = new System.Threading.ThreadStart(fnMain);
                System.Threading.Thread th = new System.Threading.Thread(ts);
                th.Name = " the thread is " + i;
                th.Start();
            }
            Console.ReadLine();
        }        public void fnMain()
        {
            IPAddress Address = Dns.GetHostByName(Dns.GetHostName()).AddressList[0];
            IPEndPoint Host = new IPEndPoint(Address, 19080);
            Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            soc.Connect(Host);
            this.fnSend(soc);
        }        public void fnSend(Socket v_socket)
        {
            double i = 0;
            while (true)
            {
                try
                {
                    string temp = i.ToString();
                    i += 1;
                    //temp = Console.ReadLine();
                    byte[] byt = System.Text.Encoding.Default.GetBytes("1");
                    //System.Threading.Thread.Sleep ( System.TimeSpan.FromSeconds (0.001));
                    Console.WriteLine(i + "          " + System.Threading.Thread.CurrentThread.Name);                    v_socket.Send(byt);
                }
                catch (Exception ex)
                {                    System.Windows.Forms.MessageBox.Show(ex.Message + " " + System.Threading.Thread.CurrentThread.Name);
                    System.Threading.Thread.CurrentThread.Abort();
                }
                //byte[] byts = new byte[10];
                //v_socket.Receive(byts);
                //Console.WriteLine(System.Text.Encoding.Default.GetString(byts));
            }            Console.WriteLine(i + "          " + System.Threading.Thread.CurrentThread.Name);
        }
    }

解决方案 »

  1.   

    总是报远程主机关闭。
    我看了,SERVER的线程没有关闭。
    客户端为什么会这样呢??
    是不是C#的SOCKET有问题.
      

  2.   


    你客户端的 发送 是如何写的,调用的 ?
     public void fnMain()
            {
                IPAddress Address = Dns.GetHostByName(Dns.GetHostName()).AddressList[0];
                IPEndPoint Host = new IPEndPoint(Address, 19080);
                Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                soc.Connect(Host);
                this.fnSend(soc);
            }
    你这里已经连上了 如果再次发 没有关闭连接  重新连接会出错的
    后面发送只要fnSend(soc);
      

  3.   

    t.fnListen();这个怎么构造函数调用了
    TCP t = new TCP();
                t.fnListen();
    也调用了
    而且你在host.Listen(20);
    之前就host.Bind()没报错吗?
      

  4.   

            public void fnMain()
            {
                IPAddress Address = Dns.GetHostByName(Dns.GetHostName()).AddressList[0];
                IPEndPoint Host = new IPEndPoint(Address, 19080);
                Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                soc.Connect(Host);
                this.fnSend(soc);
            }
    -----------------------------------------
    您能不能告诉我,您这是在干吗?
    服务器与客户机运行在同一台计算机上?
      

  5.   

    问题主要原因是因为,Tcp在没有接到数据的时候会catch出去,必须重新实例化重新连接,你报的那个错误就是这个引起的,他会默认的断开。你在catch里加入重新实例化或者重新连接,调试下看看。