Link to home
Start Free TrialLog in
Avatar of Vassilevich
Vassilevich

asked on

Soket exception 10060

I am trying figure out what might be wrong in my implementation of simple telnet test program . For some telnet i am getting the following exception :

Socket Error :10060
Telnet failed WSAEWOULDBLOCK 3

Any ideas ?


string Telnet::SendTelnetCmds(string l_uname , string l_password , string l_commands)
{

    string test;

    unsigned char buff1[2048];
    unsigned char buff[3];
    memset(buff,0,3);

    bool success = false;

    while (int len = ReadString((char* )buff,3))
    {
        if(len < 0)
        {
            int err = WSAGetLastError();
            cout <<"Socket Error :" << err <<endl;
            if(err != WSAEWOULDBLOCK)
            {
                throw exception("Telnet failed len is < 0");
            }
        }
        if (buff[0] != IAC)
        {
            break;
        }
        else if (buff[1] == DO)
        {
            buff[1] = WONT;
            WriteString((char* )buff,3);
            success = true;
        }
    }
      if(!success) throw exception("Telnet failed on IAC");
    success = false;

    while(int len = ReadString((char* )buff1,2048))
    {
        if(len < 0)
        {
            int err = WSAGetLastError();
            cout <<"Socket Error :" << err <<endl;
            if(err != WSAEWOULDBLOCK)
            {
                throw exception("Telnet failed WSAEWOULDBLOCK 1");
            }
        }
        buff1[len] = 0;
        test = (const char*)buff1;

        if(test.find("login")!=-1)
        {
            success = true;
            break;
        }
        memset(buff1,0,2048);
    }

    if(!success) throw exception("Telnet failed success 2");
    success = false;

    string nas = l_uname;
    nas+="\n";
    WriteString(nas.c_str(),nas.length());
    Sleep(500);
    while(int len = ReadString((char* )buff1,2048))
    {
        if(len < 0)
        {
            int err = WSAGetLastError();
            cout <<"Socket Error :" << err <<endl;
            if(err != WSAEWOULDBLOCK)
            {
                throw exception("Telnet failed WSAEWOULDBLOCK 2");
            }
        }
        buff1[len] = 0;
        test = (const char*)buff1;

        if(test.find("Password")!=-1)
        {
            success = true;
            break;
        }

        memset(buff1,0,2048);

        string nas1 = l_password;
        nas1+="\n";
        WriteString(nas1.c_str(),nas1.length());
    }

    if(!success) throw exception("Telnet failed success 3");
    success = false;
   
    ReadString((char* )buff1,2048);
    memset(buff1,0,2048);


    string nas2 = l_commands ;
    nas2+="\n";

    Sleep(1000);
    WriteString(nas2.c_str(),nas2.length());
    string bracket ="misha";
    string bracket2 = "$";
    string loginincorrect = "incorrect";
    int counter = 0;
    int counter1 = 0;
    string me;
    int errorcode = 0 ;
    while (int len = ReadString((char* )buff1,2048))
    {
             
        if(len < 0)
        {
            int err = WSAGetLastError();
            cout <<"Socket Error :" << err <<endl;
            if(err != WSAEWOULDBLOCK)
            {
                throw exception("Telnet failed WSAEWOULDBLOCK 3");
            }
        }
        buff1[len] = 0;
        test = (const char*)buff1;
        me.append(test);
        if(test.find(loginincorrect)==-1)
        {
            if(test.find(bracket)!=-1)
            {
                counter++;
                if(counter == 3)
                    break;
            }

            if(test.find(bracket2)!=-1)
            {
                counter1++;
                if(counter1 == 2)
                    break;
            }
        }
        else
        {
            m_nasResponseCmds = loginincorrect;
            return m_nasResponseCmds;
        }
        memset(buff1,0,2048);
    }

    m_nasResponseCmds = me;

    return m_nasResponseCmds;

}
Avatar of Salte
Salte

EWOULDBLOCK

is an error you get when you use sockets in a 'async' mode and the recv(), send() etc you call would have blocked.

It just means you are trying to receive data but there aren't any yet. You should then call select() or some such to wait until a packet receive.

If you are running in a windows program it might not be a good idea to use select() since you probably want to wake up when and if a window message arrive to your application. It is possible select can wait for windows messages also but you can also use WaitForMultipleObjects which can wait for both window messages and socket packets and that might then be just as well.

So use WaitForMultipleObjects() if you're in windows and use select() if you're not.

Alf
Salte's comments are very important.  EWOULDBLOCK just means that your thread would stop execution at ReadString.  It's important for us to know whether you are in Windows or some other OS.  If you are in Windows and are writing a single threaded application, then EWOULDBLOCK would result in the entire application freezing, hence the message.

I cannot stress this too highly.  Please, please, please take a look at Adaptive Communication Environment.

http://www.cs.wustl.edu/~schmidt/ACE.html

It is a life saver for anyone doing any type of network programming.  It is actively maintained, free, and is supported on many, many operating systems.  Seriously, it really is worth taking a look at.  There are also two books about it "C++ Network Programming" Volumes 1 & 2, which are very good reads in and of themselves.  The books explain in detail EWOULDBLOCK, what is causing it and how to resolve it in a variety of ways.

I have nothing to do with ACE other than as a user, but it has helped me and so many others I recommend it highly.

Sorry to gush, but I really think it's worth your time.

Michael
Avatar of Vassilevich

ASKER

HI Salte , the OS is win 2000 .Now i don't know much how to
tied up WaitForMultipleObjects with recv()function .Can you provide me with explanation step by step how to do it i
would realy appriciate this .

Misha.
ASKER CERTIFIED SOLUTION
Avatar of Salte
Salte

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial