.NET core replacement for TcpClient

2020-04-02 09:45发布

问题:

So in the old .NET you were able to initialize a new instance of the TcpClient class and connect to the specified port on the specified host using TcpClient(string, int). That no longer works for TcpClient in .NET core and I'm wondering if there is a replacement or some other way to do the same thing in core. I read the documentation, but maybe I'm missing something or there is a new, better way of going about this.

Also I'm going to use the GetStream method that works for both versions of TcpClient to load the stream into a NetworkStream.

回答1:

As per the source code, you can connect with:

 public Task ConnectAsync(IPAddress address, int port)

Or one of the overloads.

And the GetStream you need, at line 151:

public NetworkStream GetStream()


标签: c# .net-core