Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
C# - Code Freezes Up
Message
From
19/01/2008 16:46:03
John Baird
Coatesville, Pennsylvania, United States
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01283306
Message ID:
01283311
Views:
11
>I'm writing a little C# app that connects to a POP3 mailbox and checks for mail.
>The code freezes with a console window open at the indicated line.
>
>This is an adaption of the code found here http://www.codeproject.com/KB/IP/popapp.aspx
>
>Anyone know what's going on?
>
>
>
>/***************************************************************
> * This is the test console app
> ***************************************************************/
>static void Main(string[] args)
>{
>    int i = 0;
>
>    XMail.XMail oMail = new XMail.XMail();
>
>    oMail.sPOP3Server = "incoming.verizon.net";
>    oMail.iPort = 25;
>
>    oMail.Connect("myusername", "mypassword");
>
>}
>
>
>
>
>
>/***************************************************************
> * This is the class
> ***************************************************************/
>public class XMail
>{
>
>    private string _sPOP3Server;
>    private int _iPort;
>    private TcpClient oServer = null;
>    private Stream oStream = null;
>    private StreamReader oReader = null;
>
>
>    public bool Connect(string sLoginName, string sPassword)
>    {
>        bool bRetVal = false;
>        string sResponse;
>
>        oServer = new TcpClient(_sPOP3Server, _iPort);
>
>        oStream = oServer.GetStream();
>
>        oReader = new StreamReader(oServer.GetStream());
>
>        sResponse = oReader.ReadLine();       <== FREEZES ON THIS LINE
>
>        // More code to follow here:
>
>        return bRetVal;
>    }
>
>}
>
Acouple of things jump out right away, not sure if they could cause your problem or not...

There is no error checking for the return values for oServer, oStream and oReader. Are you sure they're being initialized correctly?

Secondly, you fill oStream by making a call to oServer.GetStream(), then you make the same call in the oReader line never using the oStream object. Could it be that oStream holds the stream and not the subsequent call to oServer.GetStream()?

Thirdly, Stream readers are usually placed in some kind of loop to test for the end of file or the presence of more characters:
using (StreamReader sr = new StreamReader(oStream)) 
{
   while (sr.Peek() >= 0) 
   {
      sResponse = sr.ReadLine());
      if (! string.IsNullOrEmpty(sResponse))
      {
          do ssomething here....
      }
   }
}
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform