ZamenWolk
ZamenWolk

Reputation: 125

UNIX socket error : socket operation on non-socket

I am trying to make a client-server communication using UNIX sockets and using the STREAM protocol.

My server is running fine, but my client is not working. Whenever I try to send or receive data, I get an error : "socket operation on non-socket". I really don't see where it comes from, because my server is very similar and I don't have any problem. My server is on my local machine (127.0.0.1) and port 5000. It is open and listening (I checked with the netstat command).

The code is there :

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>

#define CHECK(ret, mes) if ((ret) == -1) {perror(mes); exit(-1);}

#define STRING_LENGTH 250

int createSocketINETClient(short mode, char *addr, short port)
{
    int s;
    struct sockaddr_in moi, server;
    int moi_len, server_len;

    moi.sin_family = AF_INET;
    moi.sin_port = htons(port);
    moi.sin_addr.s_addr = inet_addr(addr);

    memset(moi.sin_zero, 0, 8);

    s = socket(AF_INET, mode, 0);

    CHECK(s, "socket");

    moi_len = sizeof(moi);

    CHECK(bind(s, (struct sockaddr*) &moi, moi_len), "bind");

    return s;
}

void infoSocket (int s)
{
    struct sockaddr_in sock_addr;
    socklen_t len = sizeof(sock_addr);

    getsockname(s, (struct sockaddr*) &sock_addr, &len);

    printf("Onfo of socket %d\n", s);
    printf("\t IP : %s\n", inet_ntoa(sock_addr.sin_addr));
    printf("\t port : %d\n\n", ntohs(sock_addr.sin_port));
}

int main ()
{
    int bytes;
    int sock = createSocketINETClient(SOCK_STREAM, "0.0.0.0", 0);
    struct sockaddr_in serveurSock;
    int client = 0, clientSockLen = 0;
    char message[] = "I am a message that is supposed to WORK !!!!\n";
    char fromServer[STRING_LENGTH] = "";
    infoSocket(sock);


    serveurSock.sin_family = AF_INET;
    serveurSock.sin_port = htons(5000);
    serveurSock.sin_addr.s_addr = inet_addr("127.0.0.1");

    memset(serveurSock.sin_zero, 0, 8);

    CHECK(connect(sock, (struct sockaddr*) &serveurSock, sizeof(serveurSock)), "connect");

    usleep(1000000);

    CHECK((bytes = send(client, message, sizeof(message), 0)), "send");
    printf("Message sent to server : %d bytes, \"%s\"\n", bytes, message);

    CHECK((bytes = recv(client, fromServer, sizeof(fromServer), 0)), "recv");
    printf("Message received from server : %d bytes, \"%s\"\n", bytes, fromServer);

    close(client);
    printf("Client released !\n\n");

    return 0;
}

What did I do wrong ?

EDIT : The error comes from this line :

CHECK((bytes = send(client, message, sizeof(message), 0)), "send");

Upvotes: 0

Views: 5806

Answers (2)

user237419
user237419

Reputation: 9064

you are using the "client" variable as a socket parameter to sendto() when in fact you should use the "sock" variable.

should you strace the program, you could see the following:

connect(3, {sa_family=AF_INET, sin_port=htons(5000), sin_addr=inet_addr("127.0.0.1")}, 16) = 0
nanosleep({tv_sec=1, tv_nsec=0}, NULL)  = 0
sendto(0, "I am a message that is supposed "..., 46, 0, NULL, 0) = -1 ENOTSOCK (Socket operation on non-socket)

note the first sendto() parameter which is 0 (by default this is the stdin file descriptor) when in fact it should be 3 (see connect(...) line)

as a side note, you don't need to bind() the client socket unless you have an explicit reason to do so (using some other route than the default one, bypassing a firewall rule somewhere, etc). the os will assign by default the ip of the network interface the default route goes through and a random free port.

Upvotes: 1

Yuriy Ivaskevych
Yuriy Ivaskevych

Reputation: 966

In the posted code you initialize int client = 0 and then I don't see you change it. So when you call send(0, ...) you obviously get that error.

As already mentioned by @EJP in comment to your question it can be a typo because it looks like you really was intended to use sock (as you connected it: connect(sock, ...) instead of client in call to send.

Upvotes: 0

Related Questions