The following is an example from R Stevens book "UNP". I am having difficulties
with the code shown below. 

When I try to compile it using......

        gcc tcp_connect.c -lsocket -lnsl -lresolv -lc

I get the following error messages....
                
baja{shroff}% gcc tcp_connect.c -lsocket -lnsl -lresolv -lc
tcp_connect.c: In function `tcp_connect':
tcp_connect.c:38: storage size of `hints' isn't known
tcp_connect.c:40: sizeof applied to an incomplete type
tcp_connect.c:53: dereferencing pointer to incomplete type
tcp_connect.c:53: dereferencing pointer to incomplete type
tcp_connect.c:53: dereferencing pointer to incomplete type
tcp_connect.c:56: dereferencing pointer to incomplete type
tcp_connect.c:56: dereferencing pointer to incomplete type
tcp_connect.c:61: dereferencing pointer to incomplete type

I think these errors are due to the use of getaddrinfo. I am very new to socket
programming. I'll be greatful if someone can tell me where i am going wrong.

Thanks a lot,

Chirag



#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <time.h>
#include <fcntl.h>
#include <errno.h>

//#include "addrinfo.h"

#define ERROR -1
#define OK     1 
#define TRUE   1


/********************************************************************************
* tcp_connect - client task
*
*
* RETURNS: OK or ERROR
*/

int tcp_connect (const char *host, const char *serv) {

    int sockfd, n;
    struct addrinfo     hints, *res, *ressave;

    bzero (&hints, sizeof (struct addrinfo));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;

    if ((n = getaddrinfo (host, serv, &hints, &res)) != 0) {
      printf ("tcp_connect error for %s, %s: %s\n\n",host,serv,gai_strerror(n));      
return ERROR;
    }

    ressave = res;

    do {

        if((sockfd=socket (res->ai_family,res->ai_socktype,res->ai_protocol))<0)       
     continue;

        if (connect (sockfd, res->ai_addr, res->ai_addrlen) == 0)
            break;              // connected.

        close (sockfd); // connect was not successful, so ignore this socket.

    } while ((res = res->ai_next) != NULL);


    if (res == NULL) {          // connection could not be established.
        printf ("tcp_connect error for %s, %s", host,serv);
        return ERROR;
    }

    freeaddrinfo (ressave);

    return (sockfd);
}

int main () {
 
    tcp_connect ("140.252.7.10", "daytime");

    exit (0);
}
 
 
--------------------------------------------------------------------
IETF IPng Working Group Mailing List
IPng Home Page:                      http://playground.sun.com/ipng
FTP archive:                      ftp://playground.sun.com/pub/ipng
Direct all administrative requests to [EMAIL PROTECTED]
--------------------------------------------------------------------

Reply via email to