Thread View: borland.public.cppbuilder.internet.socket
6 messages
6 total messages
Started by Eduardo Jauch
Thu, 22 Feb 2007 21:08
Problem with recv
Author: Eduardo Jauch
Date: Thu, 22 Feb 2007 21:08
Date: Thu, 22 Feb 2007 21:08
91 lines
3052 bytes
3052 bytes
Hi, I write this code (most part based on unix exemples, so, forgive me
for anything...)
#include <stdio.h>
#include <conio.h>
#include <winsock2.h>
char buffer[4000];
char buf[4000];
#pragma argsused
int main(int argc, char* argv[])
{
SOCKET sockfd;
int portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 0 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return -1;
}
if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) !=
0 ) {
WSACleanup( );
return -1;
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == INVALID_SOCKET)
{
int erro = WSAGetLastError();
printf("%d", erro);
}
else
printf("Socket inicializado...\n");
sockaddr_in addr;
addr.sin_family = AF_INET;
portno = 80;
addr.sin_port = htons(portno);
addr.sin_addr.s_addr = inet_addr("200.214.74.5");
n = connect(sockfd, (struct sockaddr*)&addr, sizeof(addr));
printf("\nResposta de connect: %d", n);
if(n != 0) return 0;
strcpy(buffer,"GET /agendamento-web/login?redirect=/agenciadores.do
HTTP/1.1\r\nHost: www.visto-eua.com.br\r\n");
n = send(sockfd, buffer, sizeof(buffer) - 1, 0);
if (n < 0)
{
printf("\nERROR writing to socket");
return 0;
}
else
printf("\nSend...OK");
n = recv(sockfd, buf, sizeof(buf) - 1, 0);
printf("\n\nEncerrado o recebimento de dados do site");
buf[n] = '\0';
printf("\n\nConte๏ฟฝdo do buffer: %s", buf);
printf("\n\nPressione qualquer tecla");
getch();
closesocket(sockfd);
return 0;
}
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.
I'm trying to read the RFC documents related to this tupe of connection
(HTTP, TCP, etc...)
I found that Host: xxx is obrigatory for HTTP 1.1 requests...
Can someone tell me if I'm forgeting to pass something on the send or if
there some error on it?
Thanks in advance :)
Re: Problem with recv
Author: 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
Re: Problem with recv
Author: Eduardo Jauch
Date: Fri, 23 Feb 2007 00:56
Date: Fri, 23 Feb 2007 00:56
75 lines
3648 bytes
3648 bytes
Remy Lebeau (TeamB) escreveu:
> You are not exiting if the socket was not created.
>
That's because i'm only begining the code :)
There lot's of things yet to implement.
>> 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.
Again, even this been an error, I'm only doing some fast tests, not
doing the code to work "really" :)
> 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.
I admit that implementing the HTTP protocol is very complex :)
But seens very funny no? ;)
I'm reading the documentation and this was the first thing I see.
But I'm not a "professional" C++ programmer. I really make some code and
some money with them, but is not my first activity.
So I really like to learning something knew :) Even something of this
complexity ;)
This was only a first try. :) I'm yet in the beginning of the lecture of
the documentation. :) I need learn much more about C++ too :)
> 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.
Well... I really was thinking that the last '\r\n' was that... I don't
know that was necessary another... I'll try it :)
> 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.
You're right :) I see this on other forum too, but I was only trying to
"receive" some data... Not the entire page. :)
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().
>
I'm read on another forum exactly the oposite... Now I'm in doubt...
Sizeof will return 4000 to me...
If the buffer is empty (full of '\0'), strlen will return 4000 too? Not 0?
> You are not taking into account that recv() can return < 0. When that
> happen, you will be accessing memory outside of your buffer.
>
I forget this... :$ hehe
> 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.
So, it's explained :)
I'll take all the tips and information seriously :)
This will help me much, I'm certain :)
Thank's Gambit :)
Re: Problem with recv
Author: Eduardo Jauch
Date: Fri, 23 Feb 2007 01:02
Date: Fri, 23 Feb 2007 01:02
3 lines
1050 bytes
1050 bytes
Add one more "\r\n" to the final worked perfectly!!! :) thanks!!! :)
Re: Problem with recv
Author: Remy Lebeau (TeamB)
Date: Fri, 23 Feb 2007 03:36
Date: Fri, 23 Feb 2007 03:36
25 lines
1836 bytes
1836 bytes
"Eduardo Jauch" <eduardo.jauch@gmail.com> wrote in message news:45de3b88@newsgroups.borland.com... > I'm read on another forum exactly the oposite... Now I'm in doubt... Your code wants to transmit 59 bytes (61 after you add the necessary CRLF) to the server, but you are telling send() to transmit 5999 bytes instead. That is completely wrong, and whoever told you otherwise is lying. > Sizeof will return 4000 to me... As it should be. That is what it is supposed to do. But that is not the correct size to use. > If the buffer is empty (full of '\0'), strlen will return 4000 too? No, it will not. It will return the actual length of the character data, up to but not including the first '\0' byte. If the buffer is completely empty, it will return 0. When you put your string data in it, it will return 59/61 instead. Gambit
Re: Problem with recv
Author: Eduardo Jauch
Date: Fri, 23 Feb 2007 14:59
Date: Fri, 23 Feb 2007 14:59
4 lines
1173 bytes
1173 bytes
Sorry. :) I was thinking of the buf (with recv, not send). Thanks :)
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads