找不到 TcpClient.GetStream 方法

本文关键字:方法 GetStream TcpClient 找不到 | 更新日期: 2023-09-27 18:33:34

我正在观看此视频以了解 C# 异步通信的基础知识。我正在使用Microsoft Visual Studio 2013。

在这一行中:

_networkStream.Add(client.GetStream());

我收到此错误:

'System.Collections.Generic.List' 不包含 'GetStream

的定义,并且找不到接受类型为 'System.Collections.Generic.List' 的第一个参数的扩展方法 'GetStream' (您是否缺少 using 指令或程序集引用?)

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

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

基于MSDN,TcpClient.GetStream方法位于命名空间System.Net.Sockets下,我已经包含该命名空间,没有错误。那么,为什么我会收到错误?

找不到 TcpClient.GetStream 方法

_clientList的实例,clientTcpClient的实例。您需要使用client.GetStream()而不是_client.GetStream()

  _networkStream.Add(client.GetStream());

我认为这是你的错字。

 _networkStream.Add(client.GetStream());