Article View: borland.public.cppbuilder.internet.socket
Article #1186Re: WSAGetLastError return always 6
From: Remy Lebeau (TeamB)
Date: Tue, 27 Feb 2007 01:44
Date: Tue, 27 Feb 2007 01:44
172 lines
6676 bytes
6676 bytes
"Eduardo Jauch" <eduardo.jauch@gmail.com> wrote in message
news:45e37c5a$1@newsgroups.borland.com...
> Maybe the cookies are set differently by server. This is the only
thing
> that could be wrong in my request.
That is entirely dependant on what the server is actualy expecting you
to send.
> Sorry, but I don't understand... How was this? What you mean with
> "write a line, write a list of lines, read a line"?
Write functions that wrap the necessary functionality, and then call
your functions instead of accessing the sockets directly. This is
particularly important with line-based protocols such as HTTP, so you
want to make sure that your core socket-level operations are as solid
and safe as possible. You can then built your protocol-level logic on
top of them. For example:
bool WriteToSocket(SOCKET sock, void *ptr, int size)
{
BYTE *pb = (BYTE*) ptr;
int iNumSent;
while( size > 0 )
{
iNumSent = send(sock, (char*) pb, size, 0);
if( iNumSent > 0 )
{
size -= iNumSent;
pb += iNumSent;
}
else
{
if( (iNumSent == 0) || (WSAGetLastError() !=
WSAEWOULDBLOCK) )
return false;
}
}
return true;
}
bool WriteStrToSocket(SOCKET sock, const char* str)
{
return WriteToSocket(sock, str, strlen(str));
}
bool WriteLineToSocket(SOCKET sock, const char *str)
{
if( WriteStrToSocket(sock, str) )
return WriteToSocket(sock, "\r\n", 2);
return false;
}
bool __fastcall TPesquisa::agendar_recv(void)
{
WriteLineToSocket(sockfd, "GET
/agendamento-web/agendamento.do?task=forwardCalendario&forward=confirm
arInformacoesDeVisto&requerenteAgendado[0].informacoesChecadas=true&ag
endamentoVO.agenciadorGrupoVO.seqAgenciadorGrupo=&locale=pt_BR&com.tel
ecom.tipo_usuario=AGENCIA+DE+VIAGEM HTTP/1.1");
WriteLineToSocket(sockfd, "Host: www.visto-eua.com.br");
WriteLineToSocket(sockfd, "User-Agent: Mozilla/5.001 (windows;
U; NT4.0; en-us) Gecko/25250101");
WriteStrToSocket(sockfd, "Cookie: ");
WriteLineToSocket(sockfd, cookies);
WriteLineToSocket(sockfd, "");
//...
}
> 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...
WinHTTP and TCppWebBrowser are not the only HTTP implementations
around. There are plenty other vendor implementations widely
available that you could try using.
> html[0] = '\0';
> bytes = send(sockfd, info_checked, strlen(info_checked), 0);
> acessos++;
> if(bytes == SOCKET_ERROR)
> {
> error_code = WSAGetLastError();
> closesocket(sockfd);
> return false;
> }
send() has the same rules as recv(), in that WSAGetLastError() could
return WSAEWOULDBLOCK which is not a failure. As shown further above,
writing should be looped just as reading is.
> strcpy(info_checked, first);
> strcat(info_checked, cookies); //<<== New cookies
> strcat(info_checked, "\r\n\r\n");
At no point in the code you showed are you parsing the server's reply
to grab the new cookies, if any. Besides that, you are not even close
to handling cookies properly anyway. You must parse the "name=value"
pairs and result their scope. You can't just send back everything the
server gives you as-is. That is not how cookies work.
> 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;
That is very sloppy and inefficient. You could just use strstr()
instead, ie:
ptr_recv = strstr(recv_buffer, "Set-Coo");
if( ptr_recv )
//...
> The function agendar_rec is inside a looping.
But it is not reading/writing correctly to begin with. You have a lot
to learn about how to use sockets properly.
> I used "zero_on_send" and "zero_on_recv" together with "acessos",
and
> find that at each 91 requests the connection is closed...
That is not a guarantee. You can't count on the number of 0 returns
you get. That is not how sockets work.
> Now I save the cookies every time recv is performed...
Not correctly, though.
> The truth is that I find that the cookies are ALWAYS the same.
If I were you, I would use an external packer sniffer, such as
Ethereal (http://www.ethereal.com), to look at what real web browsers
are actually sending/receiving, and then you can mimic that in your
code as needed.
Gambit
Technical Details
Message-ID:
<45e38c28$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<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> <45e37c5a$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 From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.internet.socket 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> <45e37c5a$1@newsgroups.borland.com> Subject: Re: WSAGetLastError return always 6 Date: Mon, 26 Feb 2007 17:44:18 -0800 Lines: 174 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 75.83.34.171 Message-ID: <45e38c28$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1172540456 75.83.34.171 (26 Feb 2007 17:40:56 -0800) Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail