Page 1 of 1

send(socket, data, strlen(data), 0) returned errors

Posted: Fri Jan 13, 2017 10:20 pm
by Wamor45
Hello,

I am using the following line in my code: r = send(socket, data, strlen(data), 0);
r is -1 and the errno is: 22.
is there a list of errornumbers with their explanation?

Thank you and regards,

Wamor

Re: send(socket, data, strlen(data), 0) returned errors

Posted: Fri Jan 13, 2017 11:48 pm
by kolban
Wamor,
See the following manual page on send():

http://man7.org/linux/man-pages/man2/send.2.html

At the end, you will see the possible symbolic definitions of error codes that can be found in the errno global. See also the function called "strerror()" that takes the value of errno as input and returns a string representation.

Most folks try and "shy away" from thinking of errno in terms of numeric values as they can legally vary from platform to platform and over time even on the same platform. If you want to see what the meaning of a specific value is at any given time on the ESP32, look in the the following file:

https://github.com/espressif/esp-idf/bl ... ys/errno.h

Re: send(socket, data, strlen(data), 0) returned errors

Posted: Sat Jan 14, 2017 3:48 pm
by Wamor45
Hello kolban,

Thank you for you explanation.

I use the following code:

Code: Select all

char *data = "Hello world";
r = send(socketnumber, data, strlen(data), 0);
ESP_LOGI(TAG, "... done sending to socket. Last read return=%d errno=%d", r, errno);
My socketnumber is obtanined like this and seems to be 0:

Code: Select all

socketnumber = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
Could this be the source of my error because the last 3 parameters could not be wrong?
Can socketnumber be 0?

Regards,

Wamor

Re: send(socket, data, strlen(data), 0) returned errors

Posted: Sat Jan 14, 2017 4:17 pm
by kolban
Howdy Wamor,
A socket number of 0 is fine ... it is merely an integer handle that represents a channel for communication and the first one you create can be 0. If socket() returned -1 then there would have been an issue. Based on the information you presented, my first guess is that you might have a mis-understanding on datagrams and sockets(). The ESP32 implements a pretty clean distribution of the sockets() API and a solid study of sockets programming outside the scope of the ESP32 would do no harm. In fact, for many tasks, actually developing your sockets logic on a PC (eg. Linux) would do no harm and the port to ESP32 should be trivial.

What I think I am seeing is that sockets() is an API on TCP/IP and TCP/IP has two distinct higher level protocols. These are TCP for connection oriented data transmission and UDP for data grams (fire and forget). The socket you created is a datagram socket. What that means is that it isn't "connected" to a partner. As such, when you wish to send a datagram, you need to instruct the environment where you wish to send it. This can be accomplished by using the sockets API called "sendto()" ... see:

https://linux.die.net/man/2/sendto

which takes a destination address (IP and port) as additional parameters as compared to send().

Now this isn't the only way to send data grams, I believe one can bind an address to a data gram socket ... but without seeing more of the logic/design, I can't say for sure what the issue may be ... but the above might be a sensible guess.