472,779 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 software developers and data experts.

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

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
0 4583

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
5
by: Sharon | last post by:
I have encountered with a most disturbing TCP problem: I have cases (too many of them) that result in data loss. Some inforamation on my test configuration: I have to PC's which are the same...
0
by: Jakob Nielsen | last post by:
This is a question about partly .net sslStreams and partly about the certificates it uses. I can't seem to find a more specific group.. and since i am coding in c# :-) I try creating a sslStream...
1
by: Chris Mullins | last post by:
We've been using the SSLStream class found in System.Net.Security to build a giant Sockets server that provides TLS encryption at the channel leve. Before .Net 2.0, we used an open-source...
5
by: Jens | last post by:
Hello, I have been looking for some C-code which listens on a user-defined port for incoming data traffic. When data is received, the data is written to a file. I found some C-code (server)...
3
by: Ryan Liu | last post by:
Will TcpClient.GetStream().Read()/ReadByte() block until at least one byte of data can be read? In a Client/Server application, what does it mean at the end of stream/no more data available? ...
3
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I'm creating an application that will read emails from GMail, using the System.Net.Sockets.TcpClient and POP protocol. However, I am having a problem with my SslStream. ...
3
by: A. W. Dunstan | last post by:
I'm creating a socket as follows: m_networkSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); m_networkSocket.LingerState = new LingerOption(true, 1);...
1
by: nevviin | last post by:
Hi, I have developed a VB.NET windows application to read mails from the server. I am using tcpclient object to read mail from mail server. I am uisng the IMAP and POP protocols for reading...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.