1

I'm writing a simple HTTP client program, it should work, but when I send a GET message there is no response from the server, after a while I get 504 Gateway timeout so the connection is established.. This is the code:

int main(int argc, char *argv[]){

int sockfd = 0, n = 0;
char recvBuff[1024];
struct sockaddr_in serv_addr; 
struct sockaddr_in my_addr;  
struct hostent *host;
char *msg=malloc(1024*sizeof(char));
socklen_t addrsize = sizeof(struct sockaddr_in );

memset(recvBuff, '0',sizeof(recvBuff));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
    printf("\n Error : Could not create socket \n");
    return 1;
} 


memset(&serv_addr, '0', sizeof(serv_addr)); 
serv_addr.sin_family = AF_INET;
if(argc==4)serv_addr.sin_port = htons(atoi(argv[3])); 
else serv_addr.sin_port = htons(80); 
host=gethostbyname(argv[1]);
bcopy(host->h_addr,&(serv_addr.sin_addr.s_addr),host->h_length);


if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
   printf("\n Error : Connect Failed. %s \n", strerror(errno));
   return 1;
} 

sprintf(msg,"GET %s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\n\r\n",argv[2],argv[1]);
printf("Sending:\n%s",msg);
write(sockfd,msg,sizeof(msg));
printf("Recieved:\n");
while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
{
    recvBuff[n] = 0;
    if(fputs(recvBuff, stdout) == EOF)
    {
        printf("\n Error : Fputs error\n");
    }
} 

if(n < 0)
{
    perror("\n Read error \n");
} 

close(sockfd);

}
eli
  • 490
  • 1
  • 3
  • 22
  • What language is this? – Angelo Fuchs May 19 '14 at 13:11
  • error 504 indicates a situation where the server you contacted does not in fact have the information you are looking for, but has to look it up itself. The latter part failed. All reasons on why this failed are to be searched on the server that you contacted and that acted as a gateway for you. Without that information this question can't be answered. – Angelo Fuchs May 19 '14 at 13:14

0 Answers0