473,326 Members | 2,095 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,326 software developers and data experts.

SslStream Read Problem

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( )...);

Now, unlike the MSDN documentation, I cannot read a <EOF>, for the POP
protocol doesn't have this, so I setup my read function as so...

byte[] buffer = new byte[2048];
int nBytesRead;
do
{
nBytesRead = stream.Read(buffer, 0, buffer.Length);
...
} while (nBytesRead != 0);

Let's pretend I authenticate, which sends a small message and the server
responds by sending "+OK Gpop ready for requests from 205.219.58.3
23903020fgae.0\r\n"

So, the first time I enter the do statement, it will read those bytes. As
nBytesRead does not equal 0, it will continue the loop. Well, now it tries
to read again, and it just hangs there until it times out.

Well, to alleviate the problem, I put a temporary break in the loop.

do
{
nBytesRead = stream.Read(buffer, 0, buffer.Length);
...
break;
} while (nBytesRead != 0);

Well, I can now read the response from GMail's pop server. This, again,
works for small messages. However, if the response from the server is
segmented, I receive only part of the message.

How can I ensure that I receive the entire message? I've tried setting the
stream's .ReadTimeout property and do a try-and-catch, but this method is
incorrect. Can anyone provide me direction into my predicament?

Thank you,
Trecius
Jul 30 '08 #1
3 11443
Trecius <Tr*****@discussions.microsoft.comwrote:
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( )...);

Now, unlike the MSDN documentation, I cannot read a <EOF>, for the POP
protocol doesn't have this, so I setup my read function as so...

byte[] buffer = new byte[2048];
int nBytesRead;
do
{
nBytesRead = stream.Read(buffer, 0, buffer.Length);
...
} while (nBytesRead != 0);

Let's pretend I authenticate, which sends a small message and the server
responds by sending "+OK Gpop ready for requests from 205.219.58.3
23903020fgae.0\r\n"

So, the first time I enter the do statement, it will read those bytes. As
nBytesRead does not equal 0, it will continue the loop. Well, now it tries
to read again, and it just hangs there until it times out.
Yes, and indeed it should. The server has indicated the end of that
particular response with a "\r\n". It can't close the stream because
you still want to be able to communicate on the same connection. Each
protocol has different ways of handling this sort of conversation flow
- some protocols force each party to say how much data they're going to
write before they do so, and others use delimiters (as is the case
here).
Well, to alleviate the problem, I put a temporary break in the loop.

do
{
nBytesRead = stream.Read(buffer, 0, buffer.Length);
...
break;
} while (nBytesRead != 0);

Well, I can now read the response from GMail's pop server. This, again,
works for small messages. However, if the response from the server is
segmented, I receive only part of the message.

How can I ensure that I receive the entire message? I've tried setting the
stream's .ReadTimeout property and do a try-and-catch, but this method is
incorrect. Can anyone provide me direction into my predicament?
Read the POP3 spec carefully to see how to detect the end of responses.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jul 30 '08 #2
Mr. Skeet:

Just a follow up on your response...

I can see that the last two characters in the message are \r\n. However,
sometimes the response from the server will contain more than one \r\n. For
example, if I RETR <Some Message #it will more than likely have \r\n in it.

So I cannot use \r\n as the end of file UNLESS it's the last two characters
in the stream. When it partitions the message, will it ensure that the last
two characters are never \r\n unless it's the LAST of the stream?

EXA:

I read 1400 bytes, the last two characters in the stream are a 64 and 72.
Of course I can see many \r\n in the stream, but because the last two bytes
are not \r\n, I should continue to read the stream. In the next stream read
I again can read a lot of \r\n; however, the last two bytes in the stream are
\r\n. As such, this is the last part in the stream.

Am I guaranteed that the message will NOT be divided on a \r\n boundary?
Trecius

"Jon Skeet [C# MVP]" wrote:
Trecius <Tr*****@discussions.microsoft.comwrote:
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( )...);

Now, unlike the MSDN documentation, I cannot read a <EOF>, for the POP
protocol doesn't have this, so I setup my read function as so...

byte[] buffer = new byte[2048];
int nBytesRead;
do
{
nBytesRead = stream.Read(buffer, 0, buffer.Length);
...
} while (nBytesRead != 0);

Let's pretend I authenticate, which sends a small message and the server
responds by sending "+OK Gpop ready for requests from 205.219.58.3
23903020fgae.0\r\n"

So, the first time I enter the do statement, it will read those bytes. As
nBytesRead does not equal 0, it will continue the loop. Well, now it tries
to read again, and it just hangs there until it times out.

Yes, and indeed it should. The server has indicated the end of that
particular response with a "\r\n". It can't close the stream because
you still want to be able to communicate on the same connection. Each
protocol has different ways of handling this sort of conversation flow
- some protocols force each party to say how much data they're going to
write before they do so, and others use delimiters (as is the case
here).
Well, to alleviate the problem, I put a temporary break in the loop.

do
{
nBytesRead = stream.Read(buffer, 0, buffer.Length);
...
break;
} while (nBytesRead != 0);

Well, I can now read the response from GMail's pop server. This, again,
works for small messages. However, if the response from the server is
segmented, I receive only part of the message.

How can I ensure that I receive the entire message? I've tried setting the
stream's .ReadTimeout property and do a try-and-catch, but this method is
incorrect. Can anyone provide me direction into my predicament?

Read the POP3 spec carefully to see how to detect the end of responses.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jul 30 '08 #3
Trecius <Tr*****@discussions.microsoft.comwrote:
Just a follow up on your response...

I can see that the last two characters in the message are \r\n. However,
sometimes the response from the server will contain more than one \r\n. For
example, if I RETR <Some Message #it will more than likely have \r\n in it.

So I cannot use \r\n as the end of file UNLESS it's the last two characters
in the stream. When it partitions the message, will it ensure that the last
two characters are never \r\n unless it's the LAST of the stream?
That depends on the piece of the protocol that you're using at the
time. Messages are terminated with a ".\r\n" (IIRC) whereas other
responses are just single lines.

You should definitely *not* base any decisions on just what *happened*
to be the last two bytes which came down the stream. That could happen
to be a packet boundary, with more data coming. You should work out
whether that "\r\n" actually signals the end of that response or not
based on the protocol.

Again, I urge you to read the POP3 RFC.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jul 30 '08 #4

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

Similar topics

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...
0
by: phplasma | last post by:
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...
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...
0
by: John | last post by:
I am using SSLStream to send data between the client and server. From the client, 1) I am looking for a way to turn off the Encryption. In SSLStream, there is a READ-ONLY Encryption property....
1
by: Dubravko Sever | last post by:
Hi, I have problem with SSLStream. I'm using it with p12 certificate and it works well when I running my code as console application (manully called). But problem starts when I tryint run it as...
1
by: Dave | last post by:
Hi. When my client program runs under XP and calls a server (solaris in this case) via an SslStream all is well. The client may make multiple calls and multiple authentication calls and be fine....
0
by: ntuyen01 | last post by:
Hi all, I want to use the SSLStream with the cipher (TLS_RSA_WITH_AES_128_CBC_SHA) to get the handshake with my server, but I not sure where I can start. I do it in C# 2.0 Here is my code: ...
0
by: Steven Blair | last post by:
Hi, Using the code example form this site: http://msdn.microsoft.com/en-us/library/system.net.security.sslstream.as px I have setup my Server and Client. My Client connects, and on the...
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.