gpt4 book ai didi

c# - SSLStream 读取无效数据 + KB3147458 SSLStream 错误(?)

转载 作者:行者123 更新时间:2023-11-30 21:49:53 24 4
gpt4 key购买 nike

当远程客户端未发送任何内容时,我遇到了 SSLStream 返回一些数据的问题。当服务器正在监听新命令时,我遇到了这个问题。如果服务器没有收到新的请求,ReadMessage() 函数应该捕获一个 IOException,因为 SSLStream 的读取超时。问题发生在第二次执行 sslStream.Read() 时,它似乎读取了客户端未发送的 5 个字节。所以问题按以下顺序发生:

-> ReadMessage() -> sslstream.Read() -> 如预期的那样捕获超时异常

-> ReadMessage() -> sslstream.Read() -> 未捕获超时异常,即使客户端未发送任何内容也读取了 5 个字节

-> ReadMessage() -> sslstream.Read() -> 如预期的那样捕获超时异常

-> ReadMessage() -> sslstream.Read() -> 未捕获超时异常,即使客户端未发送任何内容也读取了 5 个字节...

等等..

    public void ClientHandle(object obj)
{
nRetry = MAX_RETRIES;

// Open connection with the client
if (Open() == OPEN_SUCCESS)
{
String request = ReadMessage();
String response = null;

// while loop for the incoming commands from client
while (!String.IsNullOrEmpty(request))
{
Console.WriteLine("[{0}] {1}", RemoteIPAddress, request);

response = Execute(request);

// If QUIT was received, close the connection with the client
if (response.Equals(QUIT_RESPONSE))
{
// Closing connection
Console.WriteLine("[{0}] {1}", RemoteIPAddress, response);

// Send QUIT_RESPONSE then return and close this thread
SendMessage(response);
break;
}

// If another command was received, send the response to the client
if (!response.StartsWith("TIMEOUT"))
{
// Reset nRetry
nRetry = MAX_RETRIES;

if (!SendMessage(response))
{
// Couldn't send message
Close();
break;
}
}


// Wait for new input request from client
request = ReadMessage();

// If nothing was received, SslStream timeout occurred
if (String.IsNullOrEmpty(request))
{
request = "TIMEOUT";
nRetry--;

if (nRetry == 0)
{
// Close everything
Console.WriteLine("Client is unreachable. Closing client connection.");
Close();
break;
}
else
{
continue;
}
}
}

Console.WriteLine("Stopped");
}
}



public String ReadMessage()
{
if (tcpClient != null)
{
int bytes = -1;
byte[] buffer = new byte[MESSAGE_SIZE];

try
{
bytes = sslStream.Read(buffer, 0, MESSAGE_SIZE);
}
catch (ObjectDisposedException)
{
// Streams were disposed
return String.Empty;
}
catch (IOException)
{
return String.Empty;
}
catch (Exception)
{
// Some other exception occured
return String.Empty;
}

if (bytes != MESSAGE_SIZE)
{
return String.Empty;
}

// Return string read from the stream
return Encoding.Unicode.GetString(buffer, 0, MESSAGE_SIZE).Replace("\0", String.Empty);
}

return String.Empty;
}


public bool SendMessage(String message)
{
if (tcpClient != null)
{
byte[] data = CreateMessage(message);

try
{
// Write command message to the stream and send it
sslStream.Write(data, 0, MESSAGE_SIZE);
sslStream.Flush();
}
catch (ObjectDisposedException)
{
// Streamers were disposed
return false;
}
catch (IOException)
{
// Error while trying to access streams or connection timedout
return false;
}
catch (Exception)
{
return false;
}

// Data sent successfully
return true;
}

return false;
}

private byte[] CreateMessage(String message)
{
byte[] data = new byte[MESSAGE_SIZE];

byte[] messageBytes = Encoding.Unicode.GetBytes(message);

// Can't exceed MESSAGE_SIZE parameter (max message size in bytes)
if (messageBytes.Length >= MESSAGE_SIZE)
{
throw new ArgumentOutOfRangeException("message", String.Format("Message string can't be longer than {0} bytes", MESSAGE_SIZE));
}

for (int i = 0; i < messageBytes.Length; i++)
{
data[i] = messageBytes[i];
}
for (int i = messageBytes.Length; i < MESSAGE_SIZE; i++)
{
data[i] = messageBytes[messageBytes.Length - 1];
}

return data;
}

客户端也使用完全相同的 ReadMessage()、SendMessage() 和 CreateMessage() 函数向服务器发送消息。 MESSAGE_SIZE 常量也相同,设置为 2048。

最佳答案

问题是我在超时后重新使用了 SSLStream。所以我只是通过删除 nRetry 变量并设置更长的超时来解决问题。相关的 MSDN 文章说 SSLStream 将在超时异常 ( https://msdn.microsoft.com/en-us/library/system.net.security.sslstream(v=vs.110).aspx ) 后返回垃圾:

SslStream assumes that a timeout along with any other IOException when one is thrown from the inner stream will be treated as fatal by its caller. Reusing a SslStream instance after a timeout will return garbage. An application should Close the SslStream and throw an exception in these cases.

另一个问题是 Windows 更新 KB3147458(Windows 10 4 月的更新)更改了读取功能的行为。看起来 SSLStream 实现中的某些内容发生了变化,现在它每次返回 2 个部分的数据,1 个字节和其余字节。实际上 MSDN 文档并没有说 Read() 函数将一步返回所有请求的字节,并且提供的示例使用 do-while 循环来读取确切的字节数。所以我认为 Read() 函数不能保证一次读取所有请求的确切字节数,可能需要更多的读取迭代。

SSLstream 工作正常,因此没有损坏。您只需要注意并使用 do-while 循环并检查是否正确读取了所有字节。

我更改了这里显示的代码以解决我遇到的错误。

    public String ReadMessage()
{
if (tcpClient != null)
{
int bytes = -1, offset = 0;
byte[] buffer = new byte[MESSAGE_SIZE];

try
{
// perform multiple read iterations
// and check the number of bytes received
while (offset < MESSAGE_SIZE)
{
bytes = sslStream.Read(buffer, offset, MESSAGE_SIZE - offset);
offset += bytes;

if (bytes == 0)
{
return String.Empty;
}
}
}
catch (Exception)
{
// Some exception occured
return String.Empty;
}

if (offset != MESSAGE_SIZE)
{
return String.Empty;
}

// Return string read from the stream
return Encoding.Unicode.GetString(buffer, 0, MESSAGE_SIZE).Replace("\0", String.Empty);
}

return String.Empty;
}

关于c# - SSLStream 读取无效数据 + KB3147458 SSLStream 错误(?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36676593/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com