🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #1281

Re: WSAGetLastError return always 6

#1281
From: Eduardo Jauch
Date: Tue, 27 Feb 2007 00:34
228 lines
8117 bytes
Remy Lebeau (TeamB) escreveu:
>> No. My HTTP request is perfect.
> 
> If it were, you would not be getting that error from the server in the
> first place.  That error is almost always reported due to an incorrect
> request, usually becvause you left required fields out, or formatting
> them wrong.
> 

Maybe the cookies are set differently by server. This is the only thing
that could be wrong in my request.

>> Is after more than 15 iterations of the loop that I get the error.
> 
> That does not mean that the requests were perfect every time to begin
> with.

If I don't change anything at my request on every step. If the cookies
are different, so, it may be this that get incorrectly at some point...

> I would STRONGLY advise you to re-think the approach you are taking
> with your socket programming.  You are leaving your self open to all
> kinds of possible errors.  The first, and most common, one being
> buffer overflows.  You would be much better off if you wrap your
> socket code with more general operations (write a line, write a list
> of lines, read a line, etc).  Your code would become a lot more solid,
> and a lot easier to work with.

Sorry, but I don't understand... How was this? What you mean with "write
a line, write a list of lines, read a line"?

> Of course it can.  You have to look at the cookies of EVERY reply and
> then update your cache accordingly.

I'll change the code to update the cookies.

> I seriously think you need to stop trying to implement HTTP from
> scratch and use a pre-made library.  You would save yourself a LOT of
> hassle and problems.
> 

I have an application to do what I'm trying to acomplish using sockets,
that is working, using WinHTTP. I do the same application using
TCppWebBrowser. :)

With TCppWebBrowser I have a problem with memory, because IE don't free
them.

The other, using WinHTTP work perfectly, yet from time to time stop for
10-20 seconds, waiting for the server replay, and turn to work again...

>> Anyway, if I put a Sleep(5000) after test the return of recv to 0,
>> yet I continue getting the error.
> 
> The server's error reply has NOTHING to do with HOW you are
> sending/receiving the data, and EVERYTHING to do with WHAT you are
> sending.  Please show your complete request that is reporting the
> error.

This is how i send my request:

bool __fastcall TPesquisa::agendar_recv(void)
{
   int bytes;
   const char *first = "GET 
/agendamento-web/agendamento.do?task=forwardCalendario&forward=confirmarInformacoesDeVisto&requerenteAgendado[0].informacoesChecadas=true&agendamentoVO.agenciadorGrupoVO.seqAgenciadorGrupo=&locale=pt_BR&com.telecom.tipo_usuario=AGENCIA+DE+VIAGEM 
HTTP/1.1\r\nHost: www.visto-eua.com.br\r\nUser-Agent: Mozilla/5.001 
(windows; U; NT4.0; en-us) Gecko/25250101\r\nCookie: ";
   char info_checked[2049];

   strcpy(info_checked, first);
   strcat(info_checked, cookies); //<<<== cookies is: char cookies[2048]
   strcat(info_checked, "\r\n\r\n");

   for(;!Terminated;)
   {
     html[0] = '\0';
     bytes = send(sockfd, info_checked, strlen(info_checked), 0);
     acessos++;
     if(bytes == SOCKET_ERROR)
     {
       error_code = WSAGetLastError();
       closesocket(sockfd);
       return false;
     }
     else if(bytes == 0)
     {
       zero_on_send++;
       closesocket(sockfd);
       if(Login() != 0)
       {
         return false;
       }

       strcpy(info_checked, first);
       strcat(info_checked, cookies); //<<== New cookies
       strcat(info_checked, "\r\n\r\n");
     }
     else
       break;
   }

   for(;!Terminated;)
   {
     bytes = recv(sockfd, recv_buffer, sizeof(recv_buffer) - 1, 0);
     if(bytes == SOCKET_ERROR)
     {
       error_code = WSAGetLastError();
       closesocket(sockfd);
       return false;
     }
     else if(bytes == 0)
     {
       zero_on_recv++;
       closesocket(sockfd);
       if(Login() != 0)
       {
         return false;
       }
       if(CadastrarRequerente() == -1)
       {
         return false;
       }

       for(ptr_recv = &recv_buffer[0]; *ptr_recv != '\0';)
       {
         if(*ptr_recv == 'S')
         {
           ptr_recv++;
           if(*ptr_recv == 'e')
           {
             ptr_recv++;
             if(*ptr_recv == 't')
             {
               ptr_recv++;
               if(*ptr_recv == '-')
               {
                 ptr_recv++;
                 if(*ptr_recv == 'C')
                 {
                   ptr_recv++;
                   if(*ptr_recv == 'o')
                   {
                     ptr_recv++;
                     if(*ptr_recv == 'o')
                     {
                       ptr_recv += 6;
                       for(ptr_cookies = &cookies[0]; *ptr_recv != '\r'; 
ptr_cookies++, ptr_recv++)
                         *ptr_cookies = *ptr_recv;

                       *ptr_cookies = '\0';
                       break;
                     }
                   }
                 }
               }
             }
           }
         }
         else
           ptr_recv++;
       }

       for(; !Terminated ;)
       {
         html[0] = '\0';
         bytes = send(sockfd, info_checked, strlen(info_checked), 0);
         if(bytes == SOCKET_ERROR)
         {
           error_code = WSAGetLastError();
           closesocket(sockfd);
           return false;
         }
         else if(bytes == 0)
         {
           zero_on_send++;
           closesocket(sockfd);
           if(Login() != 0)
           {
             return false;
           }

           strcpy(info_checked, first);
           strcat(info_checked, cookies); //<<== New cookies
           strcat(info_checked, "\r\n\r\n");
         }
         else
           break;
       }
     }

     if(Terminated)
       return false;

     recv_buffer[bytes] = '\0';
     ptr_recv = &recv_buffer[bytes - 5];

     strcat(html, recv_buffer);

     if(strcmp(ptr_recv, "0\r\n\r\n") == 0)
       break;
   }

   return true;
}


The function agendar_rec is inside a looping.
Now, after rewriting the code (new code above), It's working.

I as missing the "CadastrarRequerente()" that must be called after every 
"Login()" to "agendar_recv()" work.

But yet i'm disconnected.

I used "zero_on_send" and "zero_on_recv" together with "acessos", and 
find that at each 91 requests the connection is closed...

Now I save the cookies every time recv is performed... It never changes.
The truth is that I find that the cookies are ALWAYS the same. Never 
changes. So, this is not the problem. I tried on other machines (with 
different IP's) and it was all the same, for the last 3 days at last...

Or is my system (windows) closing my connection for some reason, or the 
server is programmed to close the connection when reach this counter...

What do you think?
Technical Details
Message-ID: <45e37c5a$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <45e24baa@newsgroups.borland.com> <45e25047$1@newsgroups.borland.com> <45e25927$1@newsgroups.borland.com> <45e2b430$1@newsgroups.borland.com> <45e31ead$1@newsgroups.borland.com> <45e3429c$1@newsgroups.borland.com> <45e352f6$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,ead06e5a7c488245
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Mon, 26 Feb 2007 21:34:27 -0300
From: Eduardo Jauch <eduardo.jauch@gmail.com>
User-Agent: Thunderbird 1.5.0.9 (Windows/20061207)
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.internet.socket
Subject: Re: WSAGetLastError return always 6
References: <45e24baa@newsgroups.borland.com> <45e25047$1@newsgroups.borland.com> <45e25927$1@newsgroups.borland.com> <45e2b430$1@newsgroups.borland.com> <45e31ead$1@newsgroups.borland.com> <45e3429c$1@newsgroups.borland.com> <45e352f6$1@newsgroups.borland.com>
In-Reply-To: <45e352f6$1@newsgroups.borland.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 201.3.108.16
Message-ID: <45e37c5a$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1172536410 201.3.108.16 (26 Feb 2007 16:33:30 -0800)
Lines: 228
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail