Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,061 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,587 people online right now. Registration is fast and FREE... Join Now!




Disconnect/Reconnect TcpClient

 
Reply to this topicStart new topic

Disconnect/Reconnect TcpClient

cygnusX
6 Apr, 2008 - 10:25 PM
Post #1

D.I.C Head
**

Joined: 19 May, 2007
Posts: 159



Thanked: 2 times
My Contributions
I have connection with POP3 server
CODE

TcpClient client;

SslStream stream;

void Connect()
{

  client = new TcpClient();
  client.Connect("pop.gmail.com", 995);
  stream = new SslStream(client.GetStream());
  stream.AuthenticateAsClient("pop.gmail.com");
}

ReadMessages()
{
  Connect()
  //getting the message
  int length = stream.Read(readBuffer, 0, readBuffer.Length);
  message = Encoding.ASCII.GetString(readBuffer, 0, length);

  //Handle the incoming messages from the server
}


Then at some some moment i close the stream and the TcpClient and after some time i call the ReadMessages() method again.But this exception occured when i connect to the server and try to read the incoming data - "The Read method cannot be called when another read operation is pending.".What's wrong?I have closed the stream and client before initialize the new instances,what's that pending read operation?

This post has been edited by cygnusX: 6 Apr, 2008 - 10:26 PM
User is offlineProfile CardPM
+Quote Post

zakary
RE: Disconnect/Reconnect TcpClient
7 Apr, 2008 - 04:21 AM
Post #2

D.I.C Regular
Group Icon

Joined: 15 Feb, 2005
Posts: 404



Thanked: 6 times
Dream Kudos: 175
My Contributions
you have a stream.Read method called but you never close that stream, so when you try and read it again your application knows it sent it and thinks you didn't read it. So, you need to close that stream once the message has been read by calling stream.Close();

This post has been edited by zakary: 7 Apr, 2008 - 04:23 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 06:31PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month