473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 4681

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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.