各位高手啊帮帮小弟吧~
今天被Tcp的连接搞晕掉了~
写了一个打算能在学校宿舍间的局域网内传文件的程序
可是却怎么也连接不上我在两端之一用的是TcpListener
具体如下
TcpListener localListener = new TcpListener(IPAddress.Parse(172.17.77.184),8815);//这是我的ip地址和监听端口
            localListener.Start();
            if (localListener.Pending())
            {
                MessageBox.Show("there is a connection waiting");
            }
            else
            {
                MessageBox.Show("No");
            }
打算一旦监听出有连接的挂起就显示在另一端用的是TcpClient
具体如下
            IPAddress localIpAddr = IPAddress.Parse("172.17.77.178");//发送端的ip地址
            IPEndPoint localEndPoint = new IPEndPoint(localIpAddr, 6025);//从6025端口发送
            TcpClient senderClient = new TcpClient(localEndPoint);
            try
            {
                senderClient.Connect(IPAddress.Parse("172.17.77.184"), 8815);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
可是在监听端总是监听不到~在发送端总是连接超时~
请各位高手帮帮啊~!先谢了!!
请问会不会跟我们学校的网络的结构有关系呢???

解决方案 »

  1.   

    应该用 Socket socket = localListener.AcceptSocket(); 来接受请求
      

  2.   

    关键是怎么能在接受端检测到连接请求呢~是用localListener.Pending()来检测么??
      

  3.   

    使用Pending()来检测连接是非阻塞方式,在你的代码中,调用了Pending()后没有连接
    就自动进入下一步,然后退出了。自然无法接收连接。
    应该使用AcceptSocket (),这样程序运行到接收连接的地方就自动阻塞,等待连接。
    当有连接请求的时候才会返回。
      

  4.   

    To:ccjjxx001可是我的程序中的if   (localListener.Pending()) 
                            { 
                                    MessageBox.Show("there   is   a   connection   waiting"); 
                            } 
                            else 
                            { 
                                    MessageBox.Show("No"); 
                            } 
    按说应该是locallistener.Pending()可以检测是否存在挂起的请求的呀~
    我这个时候在客户端的机器上已经执行了connect的请求了~在我的服务器端还是用localListener.Pending())检测不到连接请求么?再谢了!!