Article View: borland.public.cppbuilder.internet.socket
Article #1237Re: Problem with recv
From: Remy Lebeau (TeamB)
Date: Thu, 22 Feb 2007 22:09
Date: Thu, 22 Feb 2007 22:09
54 lines
2758 bytes
2758 bytes
"Eduardo Jauch" <eduardo.jauch@gmail.com> wrote in message
news:45de060c$1@newsgroups.borland.com...
> if (sockfd == INVALID_SOCKET)
You are not exiting if the socket was not created.
> n = connect(sockfd, (struct sockaddr*)&addr, sizeof(addr));
> printf("\nResposta de connect: %d", n);
> if(n != 0) return 0;
You are not calling closesocket() or WSACleanup() if connect() fails.
> strcpy(buffer,"GET
/agendamento-web/login?redirect=/agenciadores.do
> HTTP/1.1\r\nHost: www.visto-eua.com.br\r\n");
I do not recommend implementing the HTTP protocol manually. It is a
very complex protocol. You really should stick with premade HTTP
libraries/components that do all of the work for you. For example,
you are missing a CRLF in the string above. In order for the server
to know when the headers have ended, you must send a blank line after
than, but you are not doing that.
> n = send(sockfd, buffer, sizeof(buffer) - 1, 0);
You are not taking into account that send() can return less than the
amount you asked it to send. You need to call send() in a loop until
all data has been accepted. Also, you are sending the entire buffer
(all 4000 bytes), not just the string data you copied into it. You
should be using strlen() instead of sizeof().
> n = recv(sockfd, buf, sizeof(buf) - 1, 0);
<snip>
> buf[n] = '\0';
You are not taking into account that recv() can return < 0. When that
happen, you will be accessing memory outside of your buffer.
> The problem is that to recv return pass much time (much more than
> 2 minutes while I can access the site in seconds), and always return
> n = 0.
That is because you are not terminating your HTTP request correctly,
so the server never sends a reply at all, and recv() ends up timing
out.
> I found that Host: xxx is obrigatory for HTTP 1.1 requests...
That has nothing to do with this issue.
Gambit
Technical Details
Message-ID:
<45de1497$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:
<45de060c$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,f4676ab8bfe79c79 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: <45de060c$1@newsgroups.borland.com> Subject: Re: Problem with recv Date: Thu, 22 Feb 2007 14:09:43 -0800 Lines: 56 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: <45de1497$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1172182167 75.83.34.171 (22 Feb 2007 14:09:27 -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