c++socketsclient-serverposixerrno

Invalid argument errno on connect()


I'm writing a program that basically perform server-client relationship. When i run my client (with the relevant params, when the server is already running), i get the following errno message:
Invalid argument
Here's a part of the relevant code:

if(argc != NUM_OF_ARGS)
{
    fprintf(stderr,"usage: Client <CLIENT NAME> <SERVER ADDRESS> <SERVER PORT>\n");
    exit(1);
}

int serverPort = atoi(argv[3]);

if(serverPort == 0){
    cerr << serverPort<<endl;
    fprintf(stderr,"bad arguments\n");
    exit(1);
}

//update the local machine's addr
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(serverPort);

inet_aton(argv[2],&(server_addr.sin_addr));
memset(&(server_addr.sin_zero),'\0',8);

int fd = socket(PF_INET,SOCK_STREAM,0);
if(fd < 0){
    fprintf(stderr,strerror(errno));
    exit(1);
}

if(connect(fd,(sockaddr*)&server_addr,BUFFERSIZE) < 0){
    fprintf(stderr,strerror(errno));//My program gets here and exits.
    exit(1);
}

Another note: the address i'm giving as an argument is of the format: 132.65.151.68

What am i doing wrong here?


Solution

  • connect expects as third parameter the size of the struct that the second argument points to. See man 2 connect for details.