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();
        }
    }
}

解决方案 »

  1.   

    while (soc.Connected)
                {
                    soc.Receive(buff);
                    Console.Write(Encoding.ASCII.GetString(buff));
                    soc.Shutdown(SocketShutdown.Both);
                    soc.Close();
                }
      

  2.   

    呵呵,按照楼上朋友的意思,soc只能接收一次。
    而soc在没有断开的情况下,是可以用来不断的接收的。我的问题是:当DisConnect以后,soc为什么不断的重复接收已经被接收的东西呢?????
      

  3.   

    在连接一次读取后 应该关闭当前连接 然后不断侦听client的连接请求 以便重新连接并读取数据
      

  4.   

    如果我不进行 Disconnect的话:
    这个程序会阻塞在soc.Receive(buffer)这里的呀,直到有新的消息发来为止
      

  5.   

    这个程序会阻塞在soc.Receive(buffer)这里的呀,直到有新的消息发来为止
    -------------------------------------------------------------------
    本来就是如此 你用的是同步模式而非异步模式
      

  6.   

    while (soc.Connected)
                {
                    soc.Receive(buff);
                    Console.Write(Encoding.ASCII.GetString(buff));
                    soc.Shutdown(SocketShutdown.Both);
                    soc.Close();
                }
    ----------------------------------------------------------------
    按照你的这个东西的话,如果soc要Receive两次,那么soc  = server.Accept();两次了吗
    对于tcp的话,我只要连接一次就够了呀,
    当发送方disconnect以后,soc.Connected为什么还是true呢?????????
      

  7.   

    大家有没有感觉Socket.Disconnect(true)方法有点问题
    我有时调用这个方法时,程序居然就阻塞在这个方法上不动了~~
      

  8.   

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

  9.   

    这个Disconnect是不是用了,端口可以重用啊..
    那Close呢.
    我有时写程序这样
    soc=new Socket()....
    ..
    ..//作用完close
    soc.Close();
    soc=null;//这样不知道有没有内存问题...因为我可能还要再使用一次soc对象//于是我再
    soc=new Socket()...//不知道如何换成Disconnect是不是有区别