简体   繁体   English

TcpClient的.NET核心替代品

[英].NET core replacement for TcpClient

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). 因此,在旧的.NET中,您可以初始化TcpClient类的新实例,并使用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. 这不再适用于.NET核心中的TcpClient,我想知道是否有替换或其他方式在核心中执行相同的操作。 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. 此外,我将使用适用于两个版本的TcpClient的GetStream方法将流加载到NetworkStream中。

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: 你需要的GetStream,在第151行:

public NetworkStream GetStream()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM