GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-May-2005, 14:52
robsmith robsmith is offline
New Member
 
Join Date: Mar 2005
Posts: 11
robsmith is on a distinguished road
Unhappy

Please help! Socket problem


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:

CPP / C++ / C 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);
}
Last edited by LuciWiz : 08-May-2005 at 23:11. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 09-May-2005, 01:03
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
I don't get it robsmith. 6 posts have code, 6 posts have been edited by Luciwiz to add code tags and explain them, you ignore him. 3 posts in PHP seem to have php tags... or did you forget them too?

Read the Guidelines]. Study 1.1, 1.4, All of 2, 3, and 5.
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[PROGRAM] Winsock Programming Max Payne MS Visual C++ / MFC Forum 1 08-Mar-2007 23:38
Graphic problem in Unreal Tournament 2004 zerox Computer Software Forum - Games 10 09-Oct-2005 12:31
Mother board problem mrkamran Computer Hardware Forum 2 07-Oct-2004 10:31
Apache/Tomcat/jk2 Integration Problem F@is@l Apache Web Server Forum 1 08-Sep-2004 05:29
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 07:53

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:45.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.