I am having trouble using connect() when entering struct addrinfo members as parameters.
It outputs the error:
Do I need to set something for ai_flags in the "hints" struct? Or is getaddrinfo() giving a bad value to ai_flags in the "ai" struct?
Code:
int tcpConnect() { struct addrinfo hints; struct addrinfo *ai = NULL; int res; int sock1; char * ipaddr = "127.0.0.1"; char * port = "80"; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; if ( (res = getaddrinfo(ipaddr, port, &hints, &ai)) < 0 ) { fprintf(stderr, "Error in getaddrinfo(): %s", gai_strerror(res)); return -1; } if ( (sock1 = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) < 0 ) { fprintf(stderr, "Error in socket(): %s", gai_strerror(res)); return -1; } if ( (res = connect(sock1, ai->ai_addr, ai->ai_addrlen)) < 0 ) { fprintf(stderr, "Error in connect(): %s", gai_strerror(res)); return -1; } return 0; }
Code:
Error in connect(): Bad value for ai_flags
Comment