大家帮我看看我的程序哪儿出错啦
在线等
服务端
namespace ConsoleApplication1
{
    class Program
    {       
        static void Main(string[] args)
        {
            IPAddress serverIP = IPAddress.Parse("127.0.0.1");            
            TcpListener listener = new TcpListener(serverIP,10000);
            listener.Start();
            while (true)
            {
                Socket s = listener.AcceptSocket();
                byte[] recvBytes = new byte[256];
                int bytes = 0;
                while (true)
                {
                    bytes = s.Receive(recvBytes, recvBytes.Length,0);
                    string recvString = Encoding.ASCII.GetString(recvBytes);
                    Console.WriteLine();    
                    if (bytes == 0)
                        break;  
                }
            }
        }
    }
}
客户端
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpClient client = new TcpClient();
            IPAddress serverIP = IPAddress.Parse("127.0.0.1");
            client.Connect(serverIP, 10000);
            NetworkStream stream = client.GetStream();
            string sendString = "hello";
            byte[] sendBytes = Encoding.ASCII.GetBytes(sendString.ToCharArray());
            stream.Write(sendBytes,0,sendBytes.Length);
        }
    }
}
改一下客户端也不行
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket s = new Socket(AddressFamily.InterNetwork,SocketType.stream,ProtocolType.Tcp);
            IPAddress serverIP = IPAddress.Parse("127.0.0.1");
            s.Connect(serverIP, 10000);            
            string sendString = "hello";
            byte[] sendBytes = Encoding.ASCII.GetBytes(sendString.ToCharArray());
            s.Send(sendBytes,sendBytes.Length,0);
        }
    }
}

解决方案 »

  1.   

    搞定啦
    原来是while (true)
                    {
                        bytes = s.Receive(recvBytes, recvBytes.Length,0);
                        string recvString = Encoding.ASCII.GetString(recvBytes);
                        Console.WriteLine();    
                        if (bytes == 0)
                            break;  
                    }
    出了问题
    去掉while()
    接收数据就OK啦
    因为在下一次接收的时候,客户端的连接已关闭.服务端就不能检测连接了