首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

当小弟我进行了disconnect后为什么在 Receive 这个函数上不断的接收

2012-02-10 
当我进行了disconnect后为什么在 Receive 这个函数上不断的接收usingSystemusingSystem.Collections.Gene

当我进行了disconnect后为什么在 Receive 这个函数上不断的接收
using   System;
using   System.Collections.Generic;
using   System.Text;
using   System.Net;
using   System.Net.Sockets;

namespace   ConsoleApplication1
{
        class   Program
        {
                static   void   Main(string[]   args)
                {
                        IPHostEntry   ipHost   =   Dns.Resolve(Dns.GetHostName());
                        IPAddress   ipAddr   =   ipHost.AddressList[0];
                        IPEndPoint   ipEndPoint   =   new   IPEndPoint(ipAddr,   11000);

                        Socket   client   =   new   Socket(AddressFamily.InterNetwork,
                                SocketType.Stream,   ProtocolType.Tcp);
                        Socket   server   =   new   Socket(AddressFamily.InterNetwork,
                                SocketType.Stream,   ProtocolType.Tcp);
                        server.Bind((EndPoint)ipEndPoint);
                        server.Listen(1024);
                       

                        //   Connect   the   socket   to   the   remote   end   point.
                        client.Connect(ipEndPoint);
                        Socket   soc     =   server.Accept();
                       
                                //   Send   some   data   to   the   remote   device.
                        string   data   =   "This   is   a   string   of   data   <EOF> ";
                        byte[]   buffer   =   Encoding.ASCII.GetBytes(data);

                        int   bytesTransferred   =   client.Send(buffer);

                        //   Write   to   the   console   the   number   of   bytes   transferred.
                        Console.WriteLine( "{0}   bytes   were   sent.\n ",   bytesTransferred);
                       


                        //   Release   the   socket.
                        client.Shutdown(SocketShutdown.Both);

                        client.Disconnect(false);
                        if   (client.Connected)
                                Console.WriteLine( "We 're   still   connnected ");
                        else
                                Console.WriteLine( "We 're   disconnected ");
                       
                        byte[]   buff   =   new   byte[1024];
                        while   (true)
                        {
                                soc.Receive(buff);)//在这个地方不断的会读取相同的信息
                                Console.Write(Encoding.ASCII.GetString(buff));
                        }
                       
                        Console.Read();
                }
        }
}


[解决办法]
这个用socket建立的tcp可靠连接,当一方的socket关闭时,另一方的socket也要调用close()一次吗???????????
否则:好像另一方的socket.connected还是true嘛???????????

热点排行