๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #1236

Problem with recv

#1236
From: Eduardo Jauch
Date: Thu, 22 Feb 2007 21:08
91 lines
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 :)
Technical Details
Message-ID: <45de060c$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,f4676ab8bfe79c79,start
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII
Date: Thu, 22 Feb 2007 19:08:11 -0200
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: Problem with recv 
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit
NNTP-Posting-Host: 201.89.14.159
Message-ID: <45de060c$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1172178444 201.89.14.159 (22 Feb 2007 13:07:24 -0800)
Lines: 91
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail