0

Again me, yes. 5 minute of coding = 1 day trying to fix the code. Here is my code, which throws IOException (Unable to read from stream) only on the second request, the first works fine. The exception is thrown at bytes = sslStream.Read(buffer, 0, buffer.Length); I've tried to fix this with no results.

Here is the code for request function:

public void SendRequest(string request)
{
    byte[] buffer = new byte[2048];
    int bytes = -1;
    StringBuilder message = new StringBuilder() ;

    sslStream.Write(Encoding.ASCII.GetBytes(request + "\r\n"));
    do
    {
        try
        {
            bytes = sslStream.Read(buffer, 0, buffer.Length);
            message.Append(Encoding.ASCII.GetString(buffer, 0, bytes));
        }
        catch (Exception)
        {
            throw;
        }

    } while(!message.ToString().Contains("\n"));
    Console.WriteLine(message);
}

Code calling SendRequest() function:

while((request = Console.ReadLine().ToLower()) != "exit")
{
    con.SendRequest(request);
}

If u want full code, check it here: GitHub Repo

  • Where is `sslStream` defined? – DGibbs Jul 14 '14 at 14:14
  • In same class (TcpConnector) as `SendRequest()` function, and I'm connecting with it in class' constructor (it works well). – Andrzej Sołtysik Jul 14 '14 at 14:17
  • @Endy Perhaps the request is timing out? – DGibbs Jul 14 '14 at 14:23
  • I was debugging it man, posting here is last thing I do, I've tried too google it, fix, debug. The exception says: unable to read data from transport connection. The rest isn't in english, but it's something like: "connection has been closed by software installed on computer-host". – Andrzej Sołtysik Jul 14 '14 at 14:24
  • Sorry for not posting full exception, but I thought that description is the same for every IOException :D – Andrzej Sołtysik Jul 14 '14 at 14:30
  • Could it be that the other side just did not send anything, or did not send a newline? How did you make sure this is not the case? – usr Jul 14 '14 at 14:49
  • I'm connecting to pop.gmail.com, it should answer smth like "-ERR xxxx" or "+OK" whatever you send. – Andrzej Sołtysik Jul 14 '14 at 15:24

1 Answers1

0

I know what's going on now. I'm connecting to POP3 server - after greeting message the session is in AUTHORIZATION state, when I must pass two commands: USER and PASS, if I don't pass right commands, the server closes connection (that's why I get IOException). The only 3 commands, which doesn't close the connection are propably: USER,PASS, QUIT. I hope this thread will help someone in the future :)