 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Eduardo Jauch Guest
|
Posted: Mon Feb 26, 2007 8:54 am Post subject: WSAGetLastError return always 6 |
|
|
Hi! :)
I have a loop (inside an thread) the uses send and recv to acces a web page.
All works, but after few iterations, one of the recv return 0 bytes,
what I think this is wrong, because i'm not setting the socket to
nonbloking, and the connection does not time out by server not
responding nor something like that.
Can not be the send too, because all works ok for a few iterations of
teh loop (and I don't change the HEAD method pass to send).
I'm using the recv to return only the HEAD (not using GET).
When i use WSAGetLastError, always return code 6
I search on help but not find any error code 6 returned by
WSAGetLastError...
Anyone can help me? |
|
| Back to top |
|
 |
Eduardo Jauch Guest
|
Posted: Mon Feb 26, 2007 9:10 am Post subject: Re: WSAGetLastError return always 6 |
|
|
Eduardo Jauch escreveu:
| Quote: | I search on help but not find any error code 6 returned by
WSAGetLastError...
|
I put the WSAGetLastError after the line that causes the error...
Now it allways return 0... What seens more correct, because recv is
returning 0 and not -1...
But I'm curious now...
Why is recv returning 0?
It wouldn't wait until receive some data? |
|
| Back to top |
|
 |
Eduardo Jauch Guest
|
Posted: Mon Feb 26, 2007 9:10 am Post subject: Re: WSAGetLastError return always 6 |
|
|
Eduardo Jauch escreveu:
| Quote: | Why is recv returning 0?
It wouldn't wait until receive some data?
|
I was reading that this can mean that the connection was closed...
If this is true, I need to check for 0 bytes received and
reconnect/send/recv(in loop) again?
Thanks! |
|
| Back to top |
|
 |
Eduardo Jauch Guest
|
Posted: Mon Feb 26, 2007 4:20 pm Post subject: Re: WSAGetLastError return always 6 |
|
|
Eduardo Jauch escreveu:
| Quote: | Eduardo Jauch escreveu:
Why is recv returning 0?
It wouldn't wait until receive some data?
I was reading that this can mean that the connection was closed...
If this is true, I need to check for 0 bytes received and
reconnect/send/recv(in loop) again?
Thanks!
|
Well... The answer to my question is yes...
The server is returning 500 (Internal Server Error)...
Maybe the server is set to refuse connection if the client try many
"recv" in a short time...
I'm trying to reconnect after X milliseconds...
Reconnect imediatly don't work... |
|
| Back to top |
|
 |
Eduardo Jauch Guest
|
Posted: Mon Feb 26, 2007 4:34 pm Post subject: Re: WSAGetLastError return always 6 |
|
|
Eduardo Jauch escreveu:
| Quote: |
Well... The answer to my question is yes...
The server is returning 500 (Internal Server Error)...
Maybe the server is set to refuse connection if the client try many
"recv" in a short time...
I'm trying to reconnect after X milliseconds...
Reconnect imediatly don't work...
|
No...
even if i wait 4000 milliseconds, the server does not respond...
But if I kill the thread and create it again (what takes far few from
4000 milliseconds) the server turn to respond again...
I don't think my thread is the "problem"...
But take a look anyway...
Someone have this problem before?
thanks!  |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Feb 26, 2007 11:53 pm Post subject: Re: WSAGetLastError return always 6 |
|
|
"Eduardo Jauch" <eduardo.jauch (AT) gmail (DOT) com> wrote in message
news:45e24baa (AT) newsgroups (DOT) borland.com...
| Quote: | one of the recv return 0 bytes
|
That means that the other party has closed the connection gracefully.
| Quote: | i'm not setting the socket to nonbloking and the connection does
not time out by server not responding nor something like that.
|
recv does not return 0 in non-blocking mode when a timeout occurs. It
returns SOCKET_ERROR (-1) and then WSAGetLastError() returns
WSAETIMEOUT.
| Quote: | I'm using the recv to return only the HEAD (not using GET).
|
You are probably not taking into account that the server will likely
close the connection after sending its reply to any command. That is
the default behavior for HTTP 1.0 requests. The connection is only
left open when a keep-alive is negotiated between the client and the
server as part of the command/response exchange. I'm guessing that
you are not reading the HTTP protocol documentation carefully enough,
as this is covered.
| Quote: | When i use WSAGetLastError, always return code 6
I search on help but not find any error code 6 returned by
WSAGetLastError...
|
WSAGetLastError() is only meaningful when a real error occurs. recv()
returning 0 is not an error condition.
Gambit |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Feb 26, 2007 11:54 pm Post subject: Re: WSAGetLastError return always 6 |
|
|
"Eduardo Jauch" <eduardo.jauch (AT) gmail (DOT) com> wrote in message
news:45e2b430$1 (AT) newsgroups (DOT) borland.com...
| Quote: | The server is returning 500 (Internal Server Error)...
Maybe the server is set to refuse connection if the client
try many "recv" in a short time...
|
No. Your HTTP request was malformed to begin with, so the server did
not know how to process it. 500 is a valid HTTP reply. The server is
then likely closing the connection after sending its reply.
Gambit |
|
| Back to top |
|
 |
Eduardo Jauch Guest
|
Posted: Tue Feb 27, 2007 2:28 am Post subject: Re: WSAGetLastError return always 6 |
|
|
Remy Lebeau (TeamB) escreveu:
| Quote: | Your HTTP request was malformed to begin with, so the server did
not know how to process it.
|
No. My HTTP request is perfect.
Is after more than 15 iterations of the loop that I get the error. And i
DON'T change the request any time.
I'm using this form:
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
";
char final[1024];
strcpy(final, "HTTP/1.1\r\nHost: www.visto-eua.com.br\r\nCookie: ");
strcat(final, cookies);
strcat(final, "\r\n\r\n");
cookies is a char [] variable that holds the cookies (hehe) that I get
on the first connect.
| Quote: | 500 is a valid HTTP reply. The server is
then likely closing the connection after sending its reply.
|
Yes, it is. And told me that the connection is closed.
You think that the server could change the coockies from time to time?
There a gigantic code for each session...
I'll change the code to take in account this, so refresh the cookies
variable if the cookies returned changed.
Anyway, if I put a Sleep(5000) after test the return of recv to 0, yet I
continue getting the error. But if I kill the thread and re-create it
(what takes no more tnah ONE second), all work's fine...
Any Idea? |
|
| Back to top |
|
 |
Eduardo Jauch Guest
|
Posted: Tue Feb 27, 2007 2:40 am Post subject: Re: WSAGetLastError return always 6 |
|
|
Remy Lebeau (TeamB) escreveu:
| Quote: | "Eduardo Jauch" <eduardo.jauch (AT) gmail (DOT) com> wrote in message
news:45e24baa (AT) newsgroups (DOT) borland.com...
one of the recv return 0 bytes
That means that the other party has closed the connection gracefully.
|
Yes. I find it.
The HEAD returned too says "Connection: closed"
| Quote: | recv does not return 0 in non-blocking mode when a timeout occurs. It
returns SOCKET_ERROR (-1) and then WSAGetLastError() returns
WSAETIMEOUT.
|
I'm a litle confused now...
I must use non-bloking or bloking to make the recv WAIT for the response?
| Quote: | You are probably not taking into account that the server will likely
close the connection after sending its reply to any command.
|
No. I'm not. But is because the server don't do this. Only after many
iterations (requests) it do that.
| Quote: | That is the default behavior for HTTP 1.0 requests. The connection is only
left open when a keep-alive is negotiated between the client and the
server as part of the command/response exchange. I'm guessing that
you are not reading the HTTP protocol documentation carefully enough,
as this is covered.
|
Oh, I'm reading carefully, yet I don't understand all of it of course
but I'm only begining ;)
It's exactly because this that I choose to use HTTP 1.1.
On the documentation says that the connection will keep alive for
default. But if i misunderstood, plese, tell me.
| Quote: | WSAGetLastError() is only meaningful when a real error occurs. recv()
returning 0 is not an error condition.
|
Yes. Now that I put it after the line on the recv that return the 0
byte, it always return 0.
Because i'm not using HTTP 1.0 and the request (HEAD and GET) don't
change every looping, I'm guessing that or the server is programed to
close connections that make too requests within a few time, or it is
changing the session cookies at that time...
I'm more inclinated to believe on the first.
Anyway, it don answer why I need to kill the thread to make things
working again, even if I try to reconnect and begin the whole proccess
from begining.
It's seens that my code have a problem that I not yet find...
Still looking for it...
Thanks  |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Tue Feb 27, 2007 3:35 am Post subject: Re: WSAGetLastError return always 6 |
|
|
"Eduardo Jauch" <eduardo.jauch (AT) gmail (DOT) com> wrote in message
news:45e3429c$1 (AT) newsgroups (DOT) borland.com...
| Quote: | 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.
| Quote: | 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.
| Quote: | char final[1024];
strcpy(final, "HTTP/1.1\r\nHost: www.visto-eua.com.br\r\nCookie: ");
strcat(final, cookies);
strcat(final, "\r\n\r\n");
|
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.
| Quote: | You think that the server could change the coockies from time to
time? |
Of course it can. You have to look at the cookies of EVERY reply and
then update your cache accordingly.
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.
| Quote: | 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.
Gambit |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Tue Feb 27, 2007 3:58 am Post subject: Re: WSAGetLastError return always 6 |
|
|
"Eduardo Jauch" <eduardo.jauch (AT) gmail (DOT) com> wrote in message
news:45e34585$1 (AT) newsgroups (DOT) borland.com...
| Quote: | I'm a litle confused now...
I must use non-bloking or bloking to make the recv WAIT for the
response? |
Obviously, you have not been reading up on how to use sockets properly
in the first place, or how the HTTP protocol actually works. Which is
all the more reason that you should be using a pre-made HTTP library
to do all of the hard work for you.
It does not matter whether you use a blocking or non-blocking socket.
You must continue reading from the socket until you receive a complete
blank line. Then you parse the headers that were received (in
particular the "Content-Length" and "Transfer-Encoding" headers) to
figure out how much of the remaining reply data, if any, you need to
finish reading.
If you use a blocking socket, than recv() will wait for any bytes to
arrive before exiting.
IF you use a non-blickong socket, then you have to use select() to
know when bytes are available before then calling recv(). If you do
not, then recv() will return immediately and WSAGetLastError() will
return WSAEWOULDBLOCK.
In either case, it is your responsibility to cache any data that does
get read, and then parse that data to know whether more data has to be
read again. You keep doing that in a loop until all intended data has
been received, or until an error occurs.
| Quote: | No. I'm not. But is because the server don't do this. Only after
many iterations (requests) it do that.
|
You still need to parse the reply headers. The server will tell you
whether it is keeping the socket open on its end or not. You have to
then decide whether to keep the socket open on your end as well, or to
close it for the next request. This is basic HTTP behavior that you
have to implement properly. You are sending a v1.1 request, so the
default behavior is to keep the socket open, but the server will send
a "Connection" header to confirm or reject that.
| Quote: | Because i'm not using HTTP 1.0 and the request (HEAD and GET)
don't change every looping, I'm guessing that or the server is
programed
to close connections that make too requests within a few time
|
Very unlikely. The whole point of using keep-alives is to allow
clients to make more, and faster, requests to the same server because
there is no overhead of tearing down a connection and reconnecting for
each new request.
| Quote: | or it is changing the session cookies at that time...
|
That is a possibility.
| Quote: | Anyway, it don answer why I need to kill the thread to make
things working again
|
Because you are likely managing the socket incorrectly in the first
place. That is frequently the case for people who choose to program
at the socket API level without first understanding what they are
actually doing. Socket programming is not trivial to understand, it
takes awhile to grasp it correctly.
Gambit |
|
| Back to top |
|
 |
Eduardo Jauch Guest
|
Posted: Tue Feb 27, 2007 6:34 am Post subject: Re: WSAGetLastError return always 6 |
|
|
Remy Lebeau (TeamB) escreveu:
| Quote: | 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.
| Quote: | 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...
| Quote: | 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"?
| Quote: | 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.
| Quote: | 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...
| Quote: | 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? |
|
| Back to top |
|
 |
Eduardo Jauch Guest
|
Posted: Tue Feb 27, 2007 6:53 am Post subject: Re: WSAGetLastError return always 6 |
|
|
Remy Lebeau (TeamB) escreveu:
| Quote: | Obviously, you have not been reading up on how to use sockets properly
in the first place, or how the HTTP protocol actually works. Which is
all the more reason that you should be using a pre-made HTTP library
to do all of the hard work for you.
|
Again. I'm learning to do this. And my English is not so good, what
you can see for my type errors and bad chose of words , and so,
sometimes I don't understand something that is write and begin to think
that others things that I read were misunderstunding...
But I'm reading the RFC2616 for some time now, trying to understand
completly the HTTP code. For the TCP and socket, I read the Help and
find many answars to my questions here and other foruns, including
foruns for unix and linux programing, because I'm starting to do a linux
version with almost the same code here :)
Forgive me for not understand all you write It's all my fault really.
| Quote: |
It does not matter whether you use a blocking or non-blocking socket.
You must continue reading from the socket until you receive a complete
blank line. Then you parse the headers that were received (in
particular the "Content-Length" and "Transfer-Encoding" headers) to
figure out how much of the remaining reply data, if any, you need to
finish reading.
|
Because I know the pages I'm accessing, I don't need to look to these
parameters. The page that agendar_recv() access (look to my other post),
always return a chunked page. So, I read the buffer until I get the
final like this: "0\r\n\r\n" tnat indicates the end of the response
(according with RFC2616)
And it works.
If I try to read anything after this, the recv stay many seconds waiting
fod data and finaly return 0.
| Quote: | If you use a blocking socket, than recv() will wait for any bytes to
arrive before exiting.
|
Then, I understand correct the bcb 6 help :)
| Quote: | IF you use a non-blickong socket, then you have to use select() to
know when bytes are available before then calling recv(). If you do
not, then recv() will return immediately and WSAGetLastError() will
return WSAEWOULDBLOCK.
|
This look's interesting...
I'll try a version of the code using this aproach insted bloking. To
know how my app react.
| Quote: |
In either case, it is your responsibility to cache any data that does
get read, and then parse that data to know whether more data has to be
read again. You keep doing that in a loop until all intended data has
been received, or until an error occurs.
|
ok. I do that. :)
| Quote: | You still need to parse the reply headers. The server will tell you
whether it is keeping the socket open on its end or not. You have to
then decide whether to keep the socket open on your end as well, or to
close it for the next request. This is basic HTTP behavior that you
have to implement properly. You are sending a v1.1 request, so the
default behavior is to keep the socket open, but the server will send
a "Connection" header to confirm or reject that.
|
At each 91 requests, the server close the connection... :)
| Quote: |
Very unlikely. The whole point of using keep-alives is to allow
clients to make more, and faster, requests to the same server because
there is no overhead of tearing down a connection and reconnecting for
each new request.
|
Say that to the server. Look's like he don't know that ;)
| Quote: | That is a possibility.
|
I tested this. No. The cookies are the same. Never change (see my other
post)
| Quote: |
Anyway, it don answer why I need to kill the thread to make
things working again
Because you are likely managing the socket incorrectly in the first
place. That is frequently the case for people who choose to program
at the socket API level without first understanding what they are
actually doing. Socket programming is not trivial to understand, it
takes awhile to grasp it correctly.
|
Actually I find that I forget to call a function that make a litle work
on the page, after Login(), without is impossible to get the correct
response, and was because this that I was needing kill the thread. Now
is working.
Only that the connection is closed every 91 requests...
Any Idea? |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Tue Feb 27, 2007 7:44 am Post subject: Re: WSAGetLastError return always 6 |
|
|
"Eduardo Jauch" <eduardo.jauch (AT) gmail (DOT) com> wrote in message
news:45e37c5a$1 (AT) newsgroups (DOT) borland.com...
| Quote: | 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.
| Quote: | 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, "");
//...
}
| Quote: | 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.
| Quote: | 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.
| Quote: | 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.
| Quote: | 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 )
//...
| Quote: | 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.
| Quote: | 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.
| Quote: | Now I save the cookies every time recv is performed...
|
Not correctly, though.
| Quote: | 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 |
|
| Back to top |
|
 |
Eduardo Jauch Guest
|
Posted: Tue Feb 27, 2007 7:46 am Post subject: Re: WSAGetLastError return always 6 |
|
|
Remy Lebeau (TeamB) escreveu:
| Quote: | Very unlikely. The whole point of using keep-alives is to allow
clients to make more, and faster, requests to the same server because
there is no overhead of tearing down a connection and reconnecting for
each new request.
or it is changing the session cookies at that time...
That is a possibility.
|
I modify the code to show the LAST response before recv returns zero.
Is a response ok (HTTP 200), with a Connection: close
About the cookies, I was wrong. The session cookie change every time I
start a connection (in meddle, I was looking at beggining and end on a
more than 128 caracters...). But change nomore after that, even on the
last 3 pages including that that have the connection close.
And that connection is closed always on the same count...
Seens that the server was really programed to close the connection at
predefined number of requests of the same connection.
Intriguing... |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|