找不到TcpClient.GetStream方法

时间:2014-12-09 04:25:22

标签: c# .net asynchronous tcpclient

我正在观看this video以了解有关C#异步通信的基础知识。我使用的是Microsoft Visual Studio 2013。

在这一行:

_networkStream.Add(client.GetStream());

我收到此错误:

  

' System.Collections.Generic.List'不包含' GetStream的定义,也没有扩展方法' GetStream'接受类型' System.Collections.Generic.List'的第一个参数。可以找到(你错过了使用指令或程序集引用吗?)

目前这里是我在观看教程时复制的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace Networking
{
    public class ServerManager
    {
        public int Port { get; set; }
        private TcpListener _listener;
        private List<TcpClient> _client;
        private List<NetworkStream> _networkStream;

        public ServerManager(int port)
        {
            // Assigning Port from the parameter.
            Port = port;
            // Initialize the _listener to the port specified on the constructor.
            _listener = new TcpListener(new IPEndPoint(IPAddress.Any, Port));
            // Initialize the _client sockets list.
            _client = new List<TcpClient>();
            // Initialize the _networkStream which is useful for sending and receiving data.
            _networkStream = new List<NetworkStream>();
        }

        public void Start()
        {
            // Starts the _listener to listen.
            _listener.Start();
            _listener.BeginAcceptTcpClient(new AsyncCallback(ClientConnect), null);
        }

        public void Stop()
        {
            // Closes all the _client sockets and the network stream.
            for (int i = 0; i < _client.Count; i++)
            {
                _client[i].Close();
                _networkStream[i].Close();
            }

            // Stops the listener. The socket which listens and accept incoming client sockets.
            _listener.Stop();
        }

        private void ClientConnect(IAsyncResult ar)
        {
            // When any client connects.
            // Accept the connection.
            TcpClient client = _listener.EndAcceptTcpClient(ar);
            // Add on our client list.
            _client.Add(client);
            // Add the client's stream on our network stream.
            _networkStream.Add(_client.GetStream());
            Thread threadReceive = new Thread(new ParameterizedThreadStart(ClientReceiveData), null);
            threadReceive.Start(_client.Count);

            // Accept the next connection.
            _listener.BeginAcceptTcpClient(new AsyncCallback(ClientConnect), null);
        }

        private void ClientReceiveData(object obj)
        {
            throw new NotImplementedException();
        }
    }
}

基于MSDNTcpClient.GetStream方法位于命名空间System.Net.Sockets下,我已经将其包含在内但没有错误。所以,为什么我得到错误?

2 个答案:

答案 0 :(得分:2)

_clientList的实例,clientTcpClient的实例。您需要使用client.GetStream()代替_client.GetStream()

  _networkStream.Add(client.GetStream());

答案 1 :(得分:1)

我认为这是你的错字。

 _networkStream.Add(client.GetStream());