标题:大家帮我看看我的程序哪儿出错啦
只看楼主
geqmoon
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2004-10-16
 问题点数:0 回复次数:3 
大家帮我看看我的程序哪儿出错啦
在线等
服务端
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);
        }
    }
}
搜索更多相关主题的帖子: listener serverIP TcpListener IPAddress 
2008-04-12 13:52
geqmoon
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2004-10-16
得分:0 
改一下客户端也不行
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);
        }
    }
}

2008-04-12 13:55
geqmoon
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2004-10-16
得分:0 
没人看啊~~
帮个忙啊

2008-04-12 14:23
geqmoon
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2004-10-16
得分:0 
搞定啦
原来是while (true)
                {
                    bytes = s.Receive(recvBytes, recvBytes.Length,0);
                    string recvString = Encoding.ASCII.GetString(recvBytes);
                    Console.WriteLine();   
                    if (bytes == 0)
                        break;  
                }
出了问题
去掉while()
接收数据就OK啦
因为在下一次接收的时候,客户端的连接已关闭.服务端就不能检测连接了

2008-04-12 15:42



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-208590-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.230632 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved