I am trying to create a c program that downloads a htmlo file & saves it to disk. Here is the code, each time i compile it gives this error:
c:30 Storage size of 'Their' isn't known
Please help!
Code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
/* Yes there are allot of header files but you will need them all! */
#define PORT 8080 /* This will be the port that your client will
connect to on the remote machine. If this
client wants to connect to Telnet then you
would use port 23, FTP port 21, and the list
goes on, make sure if you write a server then
you make the ports in the 4 digit range so
not to interfer with other network programs. */
#define MAXDATASIZE 1000 /* This is the maximum size of information
that can be recieved at a time from the
server. */
int main(int argc, char *argv[])
{
int sock_fd, numbytes;
/* Creates a socket file descriptor for data exchange,
numbytes holds the number of bytes recieved from
the server. */
char buffer[MAXDATASIZE];
/* This holds all the information sent from the server. */
struct hostent *he;
/* This structure is used to get the IP address from
a hostname. */
struct SockAdder Their;
/* This is the structure you created to hold information
about the stream you will be using. */
if(argc != 2)
{
/* This checks to make sure that the right
amount of arguements were passed on the
command line. */
fprintf(stderr, "usage: client hostname\n");
exit(1);
}
if((he=gethostbyname(argv[1])) == NULL)
{
herror("gethostbyname");
exit(1);
}
/* socket() -
This is always the first part of a connection.
You will notice that the connections will be built by if
statements. The above if statement should be used if you
think the user might type in a domain name like...
"www.yahoo.com" instead of an IP address. Then to create
an internet connection using the TCP protocol type this
next if statement. */
if((sock_fd=socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("socket");
exit (1);
}
/*
This is the information that will be used to make a connection.
just leave it like this everytime, unless you working with UDP
(not covered in this tutorial) then you would use SOCK_DGRAM in
place of the SOCK_STREAM, I do it this way to make it more
readable. */
Their.Family = AF_INET;
/* This is the socket address family, This text only covers
AF_INET connections so if you want more info type 'man socket'
at your linux prompt. */
Their.Port = htons(PORT);
/* This is the port your client will be connecting to. For the
most part this will always be the same. Just always remember to
define your port at the beginning of your program. Or if you want
a user specified port you could use...
unsigned int PORT = argv[3];
just after main().
Depending on the program you might have to change the htons() to
htonl() or something else, you can read up on that in a Networking
Guide. */
Their.Address = *((struct in_addr *)he->h_addr);
/* This is the address of the person your connecting to
for the most part this will stay the same too. */
bzero(&(Their.Zero), 8);
/* Don't ask just add it here everytime! */
if(connect(sock_fd, (struct sockaddr *)&Their, sizeof(struct sockaddr)) == -1)
{
/* This creates a full 'handshake' connection to
the remote host. ALL TCP CLIENT PROGRAMS WILL HAVE THIS! */
perror("connect");
exit (1);
}
if((numbytes=recv(sock_fd,buffer,MAXDATASIZE,0)) == -1)
{
/* I will explain this a bit. recv() gets information
from the server through your file descriptor(sock_fd)
and places as much as it can, determined by MAXDATASIZE,
into 'buffer'. Then the number of bytes placed into
'buffer' is placed into 'numbytes'. */
perror("recv");
exit (1);
}
buffer[numbytes] = '\0';
/* A lacking carrage return is placed at the end of the buffer. */
printf("> %s", buffer);
/* prints all information in the buffer to the screen. */
close(sock_fd);
/* closes the connection */
return (0);
}