.NET Core替代TcpClient [英] .NET core replacement for TcpClient

查看:1016
本文介绍了.NET Core替代TcpClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在旧的.NET中,您可以初始化TcpClient类的新实例,并使用TcpClient(string,int)连接到指定主机上的指定端口。 NET核心中的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). 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.

此外,我将使用GetStream方法

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

推荐答案

根据源代码,您可以连接

 public Task ConnectAsync(IPAddress address, int port)

或其中一个重载。

还有需要的GetStream,在第151行:

And the GetStream you need, at line 151:

public NetworkStream GetStream()

这篇关于.NET Core替代TcpClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆