SslStream.Read() - 返回无效数据 [英] SslStream.Read() - Returns invalid data

查看:354
本文介绍了SslStream.Read() - 返回无效数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,

我目前正在尝试使用Visual Studio C#Express使用SSL(.pem文件/认证/私钥组合)服务器来实现多线程C#套接字。

我已成功地使客户端应用程序建立连接,并发送数据,这些数据显示在服务器上的普通,去加密文本中 - 这可行。

但是,我正在"轮询"套接字以查看是否有数据可以阅读。

这里有一些合适的代码:


public void Process(int threadId)
{
int BytesRead = 0;
try
{
Master.Log(" BEGIN READ");
sslStream.ReadTimeout = 5000;
BytesRead = sslStream.Read(bytes,0,bytes.Length);

Master.Log(" READ" + BytesRead +" bytes");
if(BytesRead&g t; 0)
{
// process
}
} catch(IOException e){
Log(" Caught IOException:" + e.Message);
if(e.InnerException!= null)
Log(" =========> INNER IOException:" + e.InnerException.Message); < br>}
----



这是运行应用程序时的一些控制台输出:
---------
日志:验证成功
LOG:BEGIN READ
LOG:READ 140 bytes
LOG:[Thread 0] [Client 1] [Address 127.0.0.1] Client.ProcessDataReceived()
从客户端收到的文本:Testing消息数据! (修剪)
---------
身份验证成功的状态消息是第一行,我们只是通过AuthAsServer()进行身份验证然后尝试了sslStream.Read()并获得了140个字节数据,如'从客户端收到的文本:xxx' - 成功。

我们现在要再次进入循环以读取更多数据(如果有的话)。
-------- -
日志:开始阅读
---------
5秒后通过,例如,ReadTimeout的超时值 - 再次调用循环
没有数据可用。也就是说,客户端没有发送任何数据。
此服务器已阻止.Read(视情况而定),并且已达到超时值。
因此,服务器抛出异常 - 很好。

看看异常,然后看看它的内部io异常。
这是我得到的错误(我认为这很好),当没有数据可供查看套接字时。

---------
日志:捕获IOException:无法从传输连接中读取数据:由于连接方没有正确响应,因此联合尝试失败了一段时间,或建立的连接失败,因为连接的主机无法响应。
日志:=========> INNER IOException:连接尝试失败,因为连接请求方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机无法响应
------- -

---------
日志:开始阅读---------

现在,我们正在循环这个套接字再次 - 这里有ZERO暂停。
没有使用'timeout'值,'ReadBytes'立即返回(假设有效数据)。
'ReadBytes'值为19 - Always。我重新连接/重新启动了两个应用程序,并且返回的数据总是19个字节。但是,它不是纯文本,并且是乱码。

现在,请注意内部IO异常 - 这个与上面的不同。

---------
LOG:READ 19个字节 - 缓冲区长度为0
LOG:BEGIN READ
日志:捕获IOException:无法从传输连接读取数据:无法立即完成无阻塞的套接字操作。
日志:=========> INNER IOException:无法立即完成非阻塞套接字操作

日志:READ 19字节 - 缓冲区长度为0
LOG:BEGIN READ
LOG:捕获IOException:无法从传输连接读取数据:无法立即完成无阻塞套接字操作。
日志:=========> INNER IOException:一个非阻塞的socket操作无法立即完成
---------

上面的两个段落----重复自己并且一次又一次 - 所有的都有19个字节,永远。
没有延迟。

我想知道SslStream是否有一些'bug',它没有处理这种带有恩典的场景?
这里发生什么事?使用networkStream,这没有任何问题,即一个简单的非SSL套接字。读取没有问题,并且在没有数据的情况下适当地保持返回0字节。

任何人都可以解决这个问题吗?我对这个问题感到疯狂。

谢谢。




















C#开发人员回应这个?

我读了一些其他帖子,似乎套接字+非阻塞或其他东西存在一些问题。结果

Hey,

I am currently attempting to implement a multi-threaded C# socket, using SSL (.pem file/certification/private key combo) server using Visual Studio C# Express.

I have successfully made the client application establish a connection, and send data, which appears in plain, de-crypted text on the server - this works.

However, I am 'polling' the socket to see if data is available to read.

Here is some code which is appropriate:
----
        public void Process(int threadId)
        {
            int BytesRead = 0;
            try
            {
                Master.Log("BEGIN READ");
                sslStream.ReadTimeout = 5000;
                BytesRead = sslStream.Read(bytes, 0, bytes.Length);

                Master.Log("READ " + BytesRead + " bytes");
                if ( BytesRead > 0 )
                {
                   // process
                }
             } catch (IOException e) {
                Log("Caught IOException: " + e.Message);
                if (e.InnerException != null)
                    Log("=========> INNER IOException: " + e.InnerException.Message);
         }
----


Here is some console output when running the application:
---------
LOG: Authentication successful
LOG: BEGIN READ
LOG: READ 140 bytes
LOG: [Thread 0] [Client 1] [Address 127.0.0.1] Client.ProcessDataReceived()
Text received from client: Testing message data! (trimmed)
---------
Authentication Successful status message is the first line, we just auth'd via AuthAsServer()
Then attempted sslStream.Read() and got 140 bytes of data, as shown 'text received from client: xxx' - Success.

We are now about to enter the loop again to read more data, if any.
---------
LOG: BEGIN READ
---------
5 seconds passes, eg, the timeout value for ReadTimeout - The loop has been called again
No data is available. That is, the client has not sent any data.
This server has blocked on .Read (as appropriate), and the timeout value has been reached.
So, the server throws an exception - fine.

Take a look at the exception, and then its inner io exception.
This is the error I am getting (which is fine I think) when no data is available to be looked at in the socket.

---------
LOG: Caught IOException: Unable to read data from the transport connection: A co
nnection attempt failed because the connected party did not properly respond aft
er a period of time, or established connection failed because connected host has
 failed to respond.
LOG: =========> INNER IOException: A connection attempt failed because the conne
cted party did not properly respond after a period of time, or established conne
ction failed because connected host has failed to respond
---------

---------
LOG: BEGIN READ
---------

Now, we are looping over this socket again - There is ZERO pause here.
The 'timeout' value is not used, and the 'ReadBytes' returns instantly (supposedly with valid data).
The 'ReadBytes' value is 19 - Always. I have reconnected/restarted both applications a large number of times, and the data returned is always 19 bytes in length.
However, it is not in plain text, and is garbled.

Now, notice the inner IO Exception - this one differs from the above one.

---------
LOG: READ 19 bytes - buffer length is 0
LOG: BEGIN READ
LOG: Caught IOException: Unable to read data from the transport connection: A no
n-blocking socket operation could not be completed immediately.
LOG: =========> INNER IOException: A non-blocking socket operation could not be
completed immediately

LOG: READ 19 bytes - buffer length is 0
LOG: BEGIN READ
LOG: Caught IOException: Unable to read data from the transport connection: A no
n-blocking socket operation could not be completed immediately.
LOG: =========> INNER IOException: A non-blocking socket operation could not be
completed immediately
---------

The above two paragraphs enclosed in ----'s repeat themselves over and over again - all having 19 bytes, forever.
There is no delay.

I am wondering if there is some 'bug' with SslStream, which does not handle such a scenario with grace?
Whats happening here? With networkStream this worked without any issues, ie, a plain, non-SSL socket would .Read fine without issues, and appropriately keep returning 0 bytes when there was no data.

Can anyone shed some light here? I am going crazy about this issue.

Thank you.

- Andrew

解决方案

Can any of the C# developers respond to this?

I read a few other posts and seems there are some issues with sockets + nonblocking, or something.  


这篇关于SslStream.Read() - 返回无效数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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