By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,610 Members | 1,989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,610 IT Pros & Developers. It's quick & easy.

sslStream.Read() returns invalid data when socket times out from block/nonblock IO?

P: n/a
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.

----

To clarify on what I believe to be the bug at hand:
sslStream.Read() seems to return 19 bytes of useless/garbled data
instead of 0 bytes when there is no data to read.

Removing the sslStream object/references/usage to it, and just using
networkStream, 0 bytes is returned when no data is to be read as
expected.
---

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

Thank you.

- Andrew

Jul 5 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.