Home > Net >  C connect() function giving socket error 88
C connect() function giving socket error 88

Time:07-10

I am trying to implement a cross-platform networking program to send simple data. For windows, I am using winsock and I'm using standard linux sockets for unix/linux. The windows portion works perfectly, and so does the server part of the linux portion. The linux client fails on the connect function, giving error code 88: invalid argument. Here is the applicable code.

netsend.h

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

struct netsend_data {
    int fd, error_code, sock, new_sock, opt;
    struct sockaddr_in server, client;
};

//Must be called before anything else. 0 = success, -1 = error (check error code). If you are creating a client socket, server should equal 0. If you are creating a server socket, it should be 1
int netsend_init(struct netsend_data* data, const char* peer_ip_addr, int port, int server);

//Clients ONLY. Connects to a remote server
int netsend_connect(struct netsend_data* data);

netsend.c


//Must be called before anything else. 0 = success, -1 = error (check error code). If you are creating a client socket, server should equal 0. If you are creating a server socket, it should be 1
int netsend_init(struct netsend_data* data, const char* peer_ip_addr, int port, int server) {
    data->sock = 0;
    if (server) {   
        if (data->fd = socket(AF_INET, SOCK_STREAM, 0) < 0) {
            data->error_code = errno;
            return -1;
        }
    }
    else {  
        if (data->sock = socket(AF_INET, SOCK_STREAM, 0) < 0) {
            data->error_code = errno;
            return -1;
        }
    }
    
    //Setup info
    if (server) {   
        data->server.sin_addr.s_addr = INADDR_ANY;
    }
    else {  
        inet_pton(AF_INET, peer_ip_addr, &data->server.sin_addr);
    }
    data->opt = 1;
    data->server.sin_family = AF_INET;
    data->server.sin_port = htons(port);
    
    return 0;
}

//Clients ONLY. Connects to a remote server
int netsend_connect(struct netsend_data* data) {
    //Connect to remote server
    if (connect(data->sock, (struct sockaddr*)&data->server, sizeof(data->server)) < 0) {
        data->error_code = errno;
        return -1;
    }   
    
    return 0;
}

client.c

#include "netsend.h"
#include <stdio.h>

int main(int argc, char** argv) {   
    struct netsend_data data;   
    if (netsend_init(&data, "192.168.0.117", 8888, 0) < 0) {
        printf("Error: %i", data.error_code);
    }
    
    if (netsend_connect(&data) < 0) {
        printf("Error: %i", data.error_code);
    }
    
    if (netsend_send_data(&data, "Hello from client", 0) < 0) {
        printf("Error: %i", data.error_code);
    }
    
    char response[50];
    if (netsend_recv_data(&data, response, 50, 0) < 0) {
        printf("Error: %i", data.error_code);
    }

    printf("%s", response);
    
    netsend_close(&data);
}

Thanks in advance!

CodePudding user response:

in C, < has higher order of precedence than =. So some of your if statements are not doing what you expect.

if (data->sock = socket(AF_INET, SOCK_STREAM, 0) < 0)

The above if statement is assigning the result of the < operator to data->sock. Use parentheses to change the order of operation to get what you intended:

if ((data->sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)

Some of the other if statements have the problem and need to be fixed too.

  • Related