Thread: Please Help! Socket error

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    11

    Please Help! Socket error

    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);
    }

  2. #2
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    Windows ?

    #include<windows.h>
    #include<winsock.h>

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    11

    Re

    No, tried that, it didnt work

  4. #4
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    What system do you have? What compiler do you use?

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    Hi,

    data type for variable "Their" should be "struct sockaddr_in".

    Following line of code is not valid.

    Code:
    Their.Family = AF_INET;
    Their.Port = htons(PORT);
    unsigned int PORT = argv[3];
    Their.Address = *((struct in_addr *)he->h_addr);
    bzero(&(Their.Zero), 8);
    It should be

    Code:
    char *myipaddress;
    struct sockaddr_in Their;
    
    if((he=gethostbyname(argv[1])) == NULL)
    {
    	herror("gethostbyname");
    	exit(1);
    }
    
    Their.sin_family=AF_INET;
    Their.sin_port=htons(PORT);
    myipaddress=(char *)malloc(sizeof(char)*strlen(inet_ntoa(*(struct in_addr *) (he->h_addr_list[0])))+1);
    strcpy(myipaddress,inet_ntoa(*(struct in_addr *) (he->h_addr_list[0])));
    Their.sin_addr.s_addr=inet_addr(myipaddress);
    free(myipaddress);
    memset(&(Their.sin_zero),'\0',8);

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    11

    Unhappy Re

    Hi thanks alot for your help,

    I changed the code but got these errors:

    tcp.c: In function `main':
    tcp.c:78: parse error before `char'
    tcp.c:88: `Their' undeclared (first use in this function)
    tcp.c:88: (Each undeclared identifier is reported only once
    tcp.c:88: for each function it appears in.)
    tcp.c:90: `myipaddress' undeclared (first use in this function)
    tcp.c:90: warning: passing arg 1 of `strlen' makes pointer from integer without a cast
    tcp.c:91: warning: passing arg 2 of `strcpy' makes pointer from integer without a cast

    Heres the new code:

    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. */
        char *myipaddress;
    
    struct sockaddr_in Their;
    
    if((he=gethostbyname(argv[1])) == NULL)
    {
    	herror("gethostbyname");
    	exit(1);
    }
    
    Their.sin_family=AF_INET;
    Their.sin_port=htons(PORT);
    myipaddress=(char *)malloc(sizeof(char)*strlen(inet_ntoa(*(struct in_addr *) (he->h_addr_list[0])))+1);
    strcpy(myipaddress,inet_ntoa(*(struct in_addr *) (he->h_addr_list[0])));
    Their.sin_addr.s_addr=inet_addr(myipaddress);
    free(myipaddress);
    memset(&(Their.sin_zero),'\0',8);
    
    
    	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);
    }

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    errors are pretty straingtforward. Check the syntax at errored lines.

    Your complier does not understand C++ style variable declaration.

    Add variable declaration for "myipaddress" and "Their" at the beginining of main() and not in the middle of code.

    Also, variable "Their" is declared twice. Once in your old code and once again in your new code which I gave you.

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    11

    Re

    OK thank you
    Changed that, but i now get this error:

    stonkin:4> gcc -lsocket -lnsl tcp.c
    tcp.c: In function `main':
    tcp.c:89: warning: passing arg 1 of `strlen' makes pointer from integer without a cast
    tcp.c:90: warning: passing arg 2 of `strcpy' makes pointer from integer without a cast
    Undefined first referenced
    symbol in file
    herror /var/tmp/ccExuqbr.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status


    Bit baffled by that one , sorry to keep going on, here the code:


    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[])
    {
        char *myipaddress;
        struct sockaddr_in Their;
    
    	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. */
    
    
    	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. */
    
    
    if((he=gethostbyname(argv[1])) == NULL)
    {
    	herror("gethostbyname");
    	exit(1);
    }
    
    Their.sin_family=AF_INET;
    Their.sin_port=htons(PORT);
    myipaddress=(char *)malloc(sizeof(char)*strlen(inet_ntoa(*(struct in_addr *) (he->h_addr_list[0])))+1);
    strcpy(myipaddress,inet_ntoa(*(struct in_addr *) (he->h_addr_list[0])));
    Their.sin_addr.s_addr=inet_addr(myipaddress);
    free(myipaddress);
    memset(&(Their.sin_zero),'\0',8);
    
    	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);
    }

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    I presume you are compiling on Solaris.
    try the complie instructions of this page

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,056
    > myipaddress=(char *)malloc(sizeof(char)*strlen(inet_ntoa(*(struct in_addr *) (he->h_addr_list[0])))+1);
    > strcpy(myipaddress,inet_ntoa(*(struct in_addr *) (he->h_addr_list[0])));
    Check the return type of inet_ntoa - it returns an int, not a char* (according to the warnings).

    Also, simplyfy the code by making a separate statement of the inet_ntoa bit.
    Also, there is no need to cast malloc, this is C remember (see the FAQ)

    > gcc -lsocket -lnsl tcp.c
    Command line parameters are order sensitive.
    You're searching libraries BEFORE you have any symbols to look for.

    Try
    gcc tcp.c -lsocket -lnsl
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM