473,883 Members | 1,684 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


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;
Master.Log("BEG IN READ");
sslStream.ReadT imeout = 5000;
BytesRead = sslStream.Read( bytes, 0, bytes.Length);

Master.Log("REA D " + BytesRead + " bytes");
if ( BytesRead 0 )
// process
} catch (IOException e) {
Log("Caught IOException: " + e.Message);
if (e.InnerExcepti on != null)
Log("=========I NNER IOException: " +
e.InnerExceptio n.Message);
Here is some console output when running the application:
LOG: Authentication successful
LOG: READ 140 bytes
LOG: [Thread 0] [Client 1] [Address]
Client.ProcessD ataReceived()
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.
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


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

LOG: READ 19 bytes - buffer length is 0
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: 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

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

Thank you.

- Andrew

Jul 5 '06 #1
0 4770

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

Similar topics

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) p.stdin.write("hostname\n") however, it doesn't seem to work. I think the cmd.exe is catching it.
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 (Dell Dimension 2400). PC1 is the tcp server, PC2 is the tcp client, they two configured to 65535 bytes tco buffer/window, and working in synchronous manner while for each client the server start a dedicated thread. The client send the data size in...
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 from a regular networkstream as folows Socket clientSocket = serverSocket.EndAccept(result); clientSocket.Blocking = true; Stream clientStream = new NetworkStream(clientSocket); SslStream sslStream = new SslStream(clientStream);
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 encryption channel from Mentalis, and have even looked at the Mono implementation for doing this. The problem comes from the SSLStream not doing any buffer management. None. Zero. In the "no buffer management" case, each SSLStream allocates bufferes...
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) that almost does the job. It listens on a user-defined port and responds to incoming data by writing how many times somebody has tried to connect to the server.
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? Client could send data once few seconds of minutes. Is there an "end" at all? In a C/S application, if server side call BeginginRead() again in EndRead() to create a endless loop to get message from client, is this a better approach than "one...
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. After I create the TcpClient, I create my SslStream... stream = new System.Net.Security.SslStream(tcpclient.GetStream()...);
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); m_networkSocket.Blocking = true; m_networkSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, true); m_networkSocket.SetSocketOption(SocketOptionLevel.Socket,
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 the mails. For this i used to send command using write command of ssl stream and read the response using the reader Dim sslstream As Net.Security.SslStream
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.