首页 > 解决方案 > NetworkStream.Read() 在短暂不活动后连接到 netcat 实例抛出 IOException?

问题描述

我有一个NetworkStream连接到远程netcat实例的对象。一切正常,而我在键盘上打字并且数据在主机之间传输。

$serverStream.Read($bytes, 0, $bytes.Length))

但是,大约 60 秒不活动后,我IOExceptionRead(). 阅读文档告诉我IOException,当套接字关闭时抛出。

我用netcat了很多,从来没有经历过它会断开会话。NetworkStream所以,我猜这个问题与?

如何防止$serverStream.Read()抛出异常,而只是等到数据可用(保持会话完整)?

网络流

https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.networkstream.read?view=netframework-4.8

Exception calling "Read" with "3" argument(s): "Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or establishe
d connection failed because connected host has failed to respond."
At C:\Users\bd6164\Desktop\phishing_revsh.ps1:31 char:9
+ while (($i = $serverStream.Read($bytes, 0, $bytes.Length)) -ne 0) {
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : IOException

标签: c#.netpowershell

解决方案


网猫

你确定你没有设置超时?

网络流

您可以使用以下行设置网络流的读取超时:

networkStream.ReadTimeout =毫秒


也许这会帮助你:

https://unix.stackexchange.com/questions/424125/ncat-command-gives-idle-timeout-expired-on-using-option-i


推荐阅读