I have a problem with the recv() function. I wrote a simple program studying Beej Guide and made a simple program that connects to yahoo.com on port 80 and runs the GET HTML command. Now doing this with netcat or telnet displays a lot of garbage data, actually retrieves the webpage for me. But using my program just retrieves a small amount of data. Can anyone help? Here is the code
Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <strings.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#define DEST_PORT 80
#define DEST_IP "68.142.197.74"
#define MAXDATASIZE 300
int main()
{
int numbytes;
char buff[MAXDATASIZE];
int my_socket; //creeaza variabila ce va stoca socket-ul
struct sockaddr_in destination_address; //stocheaza adresa de conectare
if ((my_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
}
destination_address.sin_family = AF_INET; //host to byte order
destination_address.sin_port = htons(DEST_PORT); //short, network to byte order
destination_address.sin_addr.s_addr = inet_addr(DEST_IP); //destination ip
bzero(&(destination_address.sin_zero), 8); //zero the rest of the struct
if ((connect(my_socket, (struct sockaddr*)&destination_address, sizeof(struct sockaddr))) == -1)
{
perror("connect");
exit(1);
}
if (send(my_socket, "GET HTML\n",9, 0) == -1)
{
perror("send");
exit(1);
}
if ((numbytes = recv(my_socket, buff, MAXDATASIZE, 0)) == -1)
{
perror("recv");
exit(1);
}
buff[numbytes] = '\0';
printf("Received: %s", buff);
close(my_socket);
return 0;
}