发送数据端使用完NetworkStream是否Close()了?

解决方案 »

  1.   

    .。。在TextBox3.Text += RecMessage + "\r\n"后;这加么?还是接收不到数据。。
      

  2.   

    我的意思是你的客户端 networkstream发送数据后如果有Close方法 你服务器端接收完数据就断开连接了 networkstream.Close() 方法会断开连接的
      

  3.   

    客户端是一个封装了的软件,我下了一个做好小服务器,可以连接发送接收数据。我加了while不是就一直无限循环了么。。为什么接收不到数据
      

  4.   

    NetworkStream netStream = new NetworkStream(accSock);
    while (check)
    {....
      

  5.   

    accSock=Sock.Accept();
    你才接受一次呀,你要一直监听才可以!strHostName = Dns.GetHostName();
    IPHostEntry ipEntry = Dns.GetHostByName( strHostName );
    aryLocalAddr = ipEntry.AddressList;

    Socket listener = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
    listener.Bind( new IPEndPoint( aryLocalAddr[0], 399 ) );
    //listener.Bind( new IPEndPoint( IPAddress.Loopback, 399 ) ); // For use with localhost 127.0.0.1
    listener.Listen( 10 );// Setup a callback to be notified of connection requests
    listener.BeginAccept( new AsyncCallback( app.OnConnectRequest ), listener );



    /// <summary>
    /// Callback used when a client requests a connection. 
    /// Accpet the connection, adding it to our list and setup to 
    /// accept more connections.
    /// </summary>
    /// <param name="ar"></param>
    public void OnConnectRequest( IAsyncResult ar )
    {
    Socket listener = (Socket)ar.AsyncState;
    NewConnection( listener.EndAccept( ar ) );
    listener.BeginAccept( new AsyncCallback( OnConnectRequest ), listener );
    }
    /// <summary>
    /// Add the given connection to our list of clients
    /// Note we have a new friend
    /// Send a welcome to the new client
    /// Setup a callback to recieve data
    /// </summary>
    /// <param name="sockClient">Connection to keep</param>
    //public void NewConnection( TcpListener listener )
    public void NewConnection( Socket sockClient )
    {
    // Program blocks on Accept() until a client connects.
    //SocketChatClient client = new SocketChatClient( listener.AcceptSocket() );
    SocketChatClient client = new SocketChatClient( sockClient );
    m_aryClients.Add( client );
    Console.WriteLine( "Client {0}, joined", client.Sock.RemoteEndPoint ); // Get current date and time.
    DateTime now = DateTime.Now;
    String strDateLine = "Welcome " + now.ToString("G") + "\n\r"; // Convert to byte array and send.
    Byte[] byteDateLine = System.Text.Encoding.ASCII.GetBytes( strDateLine.ToCharArray() );
    client.Sock.Send( byteDateLine, byteDateLine.Length, 0 ); client.SetupRecieveCallback( this );
    } /// <summary>
    /// Get the new data and send it out to all other connections. 
    /// Note: If not data was recieved the connection has probably 
    /// died.
    /// </summary>
    /// <param name="ar"></param>
    public void OnRecievedData( IAsyncResult ar )
    {
    SocketChatClient client = (SocketChatClient)ar.AsyncState;
    byte [] aryRet = client.GetRecievedData( ar ); // If no data was recieved then the connection is probably dead
    if( aryRet.Length < 1 )
    {
    Console.WriteLine( "Client {0}, disconnected", client.Sock.RemoteEndPoint );
    client.Sock.Close();
    m_aryClients.Remove( client );      
    return;
    } // Send the recieved data to all clients (including sender for echo)
    foreach( SocketChatClient clientSend in m_aryClients )
    {
    try
    {
    clientSend.Sock.Send( aryRet );
    }
    catch
    {
    // If the send fails the close the connection
    Console.WriteLine( "Send to client {0} failed", client.Sock.RemoteEndPoint );
    clientSend.Sock.Close();
    m_aryClients.Remove( client );
    return;
    }
    }
    client.SetupRecieveCallback( this );
    }
      

  6.   

    这个是异步的吧,这个还没看。。有没同步的
    # mngzilin
    # (海风)。。我试了没用额
      

  7.   

    MyServer=new IPEndPoint(myIP,Int32.Parse(TextBox1.Text));
            Sock=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            Sock.Bind(MyServer);
            Sock.Listen(50);
           while (check)
            {
            accSock=Sock.Accept();
            if (accSock.Connected)
            {            Byte[] Rec = new Byte[64];
                NetworkStream netStream = new NetworkStream(accSock);
                netStream.Read(Rec, 0, Rec.Length);
                string RecMessage = System.Text.Encoding.GetEncoding("gb2312").GetString(Rec);
                TextBox3.Text += RecMessage;        } 
                
            }
        }
    把sock。accept放进循环也接收不到数据。。
      

  8.   

     accSock=Sock.Accept();
    放进循环是为了接收多个客户端的连接
    Byte[] Rec = new Byte[64];
      NetworkStream netStream = new NetworkStream(accSock);
      netStream.Read(Rec, 0, Rec.Length);
      string RecMessage = System.Text.Encoding.GetEncoding("gb2312").GetString(Rec);
      TextBox3.Text += RecMessage;
    放进循环是多次接收一个客户端的信息,一般多客户端服务器端会启多线程接收信息,你现在将接收的语句放在循环中就行,你试试看