Sunday, 8 September 2013

Invalid Argument: connect() C socket programming

Invalid Argument: connect() C socket programming

I'm working on a C program that creates a connection between a client and
server. When I run connect on the socket I've already created I keep
getting an error that I'm passing an invalid argument.
Any help would be awesome!
The code for the client function:
void client(char* ipAddress, char* serverPort){
int status;
//server addrinfo
struct addrinfo serverHints, *serverRes;
int socketDescriptor;
int addressLength;
memset(&hints, 0, sizeof hints); // make sure the struct is empty
hints.ai_family = AF_UNSPEC; // don't care IPv4 or IPv6
hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
hints.ai_flags = AI_PASSIVE; // fill in my IP for me
//setup client socket
if ((status = getaddrinfo(ipAddress, serverPort, &hints, &res)) != 0) {
printf("%s \n", "This error above");
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
exit(1);
}
if((socketDescriptor = socket(res->ai_family, res->ai_socktype,
res->ai_protocol)) ==-1){
perror("client: socket");
}
addressLength = sizeof hints;
if(connect(socketDescriptor, res->ai_addr, addressLength)==-1){
close(socketDescriptor);
perror("client: connect");
}
}

No comments:

Post a Comment