PrathapB
PrathapB

Reputation: 382

Socket operation on non socket error in C

I am new to socket programming... I tried this server side program

#define BUFLEN 512
#define MYPORT 3456
void errorp(char* msg)
{
        perror(msg);
        exit(1);
}

int main()
{
        struct sockaddr_in server, client;
        int sock;
        int slen = sizeof(server);
        int clen = sizeof(client);
        char *recvbuf, senbuf[BUFLEN] = {'h','e','l','l','o'};

        if((sock = socket(AF_INET, SOCK_DGRAM, 0) == -1))
                errorp("Socket creation failed");


        printf("To the client: %s, %s", senbuf, " World");
        bzero(&server, sizeof(server));

        server.sin_family = AF_INET;
        server.sin_port = MYPORT;
        server.sin_addr.s_addr = inet_addr("127.0.0.1");
        if(bind(sock, (struct sockaddr*)&server, slen)==-1)
                errorp("Socket Bind Failed");
        if(recvfrom(sock, recvbuf, sizeof(recvbuf), 0, (struct sockaddr*) &client, &clen) == -1)
                errorp("recv from error");
        printf("From the client: %s", recvbuf);

        if(sendto(sock, senbuf, sizeof(senbuf), 0, (struct sockaddr*) &client, sizeof(client)) == -1)
                errorp("Error in sending");

        printf("To the client: %s", senbuf);
        close(sock);
        return 0;
}

There are no compilation errors but the output is

Socket Bind Failed: Socket operation on non-socket
To the client: hello,  World

Please help me figure out where the mistake is? and help get rid of it

Upvotes: 5

Views: 37301

Answers (2)

alk
alk

Reputation: 70981

The error message says it all: The socket isn't a (valid) socket.

This should make you look at the code creating the socket:

if((sock = socket(AF_INET, SOCK_DGRAM, 0) == -1))

The code above 1st compares the result of the call to socket() to -1 and then assigns the result of the comparison to sock. So it's either 0 or 1. And the result of the call to socket() is lost.

The code shall look like this:

if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)

as == binds tighter then =.


BTW, having used a Yoda-Conditition would have avoided such kind of "typo":

if (-1 == (sock = socket(AF_INET, SOCK_DGRAM, 0)))

Also at least clen shall be of type socklen_t as its address is passed, to have a value written into it, which will fail miserably if the size of the expected socklen_t would be different from an int (which the code shown passes).

Upvotes: 19

paxdiablo
paxdiablo

Reputation: 882426

if((sock = socket(AF_INET, SOCK_DGRAM, 0) == -1))
//         \__________________________________/

You have your brackets in the wrong place. It's setting sock to a true/false value because == is "more binding" than =. It should instead be:

if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
//  \_____________________________________/

which sets sock to the return value from socket() and then compares that to -1.

You also have no backing storage for recvbuf which means your recvfrom(), once it starts working, will almost certainly do something bad.

Upvotes: 11

Related Questions