Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket operation on non socket error in C

Tags:

c

sockets

udp

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

like image 399
PrathapB Avatar asked Aug 31 '14 10:08

PrathapB


People also ask

What is socket operation on non socket error?

The “Socket operation on a non-socket” error means that, for some reason, the Windows TCP-IP stack has been overloaded, and the socket channel (which is used for communicating with the Internet) has been shut down abruptly.

What causes a socket error?

Socket errors can be caused by various issues including connectivity problems on the network, client or server computers or due to a firewall, antivirus or a proxy server. This error occurs when the socket connection to the remote server is denied.

What does socket () do in C?

The socket() function shall create an unbound socket in a communications domain, and return a file descriptor that can be used in later function calls that operate on sockets. The socket() function takes the following arguments: domain. Specifies the communications domain in which a socket is to be created.

What is a socket operation?

Definition: A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to. An endpoint is a combination of an IP address and a port number.


1 Answers

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).

like image 119
alk Avatar answered Oct 05 '22 00:10

alk