Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
C# - Code Freezes Up
Message
De
19/01/2008 16:46:03
John Baird
Coatesville, Pennsylvanie, États-Unis
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01283306
Message ID:
01283311
Vues:
14
>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....
      }
   }
}
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform