TcpClient 的 .NET 核心替换 [英] .NET core replacement for TcpClient

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

问题描述

因此,在旧的 .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.

此外,我将使用适用于两个版本的 TcpClient 的 GetStream 方法将流加载到 NetworkStream 中.

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

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

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