Article View: borland.public.cppbuilder.internet.socket
Article #1191Re: WSAGetLastError return always 6
From: Eduardo Jauch
Date: Tue, 27 Feb 2007 03:56
Date: Tue, 27 Feb 2007 03:56
238 lines
9011 bytes
9011 bytes
Remy Lebeau (TeamB) escreveu:
> "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.
>
Well, the cookies aren't the problem anyway :) But I'll try the app you
mentioned on the end to see if I'm correct.
>> 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 understand now :)
This is really good advice :)
But I'll try to see if this affect the performance...
If affect, I'll do it, but after shure that the TCP layer is ok, I'll
put the code "inline". Few calls, more fast... I think :)
>> 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.
For now, I'll try other 3. Indy and other two that I don't remember. But
don't get to install them. All of them presented some type of problem
during installation...
Soon I'll try again Indy. Has a lot of functionality. But I'll continue
to trying to use sockets directly anyway :)
>
>> 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.
>
Hum... I forget about this... :) I have read on other forum, but at that
time, I'll barely got to connect...
And this can cause a grate trouble... I'll change the code to take it in
account.
>> 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.
You're right, like always ;)
I will use Ethereal to see if some of the cookies are not been send back.
If not, I'll implement a code to send back only what is needed :)
>
>> 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 )
> //...
>
I tried this. :) But believe or not, my code works and is much faster
than using strstr. :)
Is not beauty or elegant, and I'm seeking for a more fast to do this,
but for now it works :)
I use the same "schema" at many points on the code :)
Give me some trouble on the begining, but after some tests I put to work :)
>> 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'm shure of that :)
>> 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.
>
I'm not using it. I only do a test, to see if my suspicious wasn't
wrong. Really, The close always happen on the same interval...
Can be something wrong with cookis, or with the buffer overflow that you
mentioned. I'll find if is this or the server really do this because he
want...
>> Now I save the cookies every time recv is performed...
>
> Not correctly, though.
>
Endeed, but it's working, for now at least :)
>> 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.
>
In the beginning I use the OnBeforeNavigate event of the TCppWebBrowser
to see that... :)
But I will try this app. Looks pretty gook :)
>
> Gambit
Thanks Gambit :)
>
>
Technical Details
Message-ID:
<45e3abbc@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!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> <45e38c28$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: Tue, 27 Feb 2007 00:56:37 -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> <45e37c5a$1@newsgroups.borland.com> <45e38c28$1@newsgroups.borland.com> In-Reply-To: <45e38c28$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: <45e3abbc@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1172548540 201.3.108.16 (26 Feb 2007 19:55:40 -0800) Lines: 238 Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail