Using the sockets API I get an error related to sockaddr_in
Posted: Sat Jul 13, 2024 6:31 pm
Mainly for fun I wanted to see if I could write a server using the good old BSD sockets API. This appears to be supported on the ESP32 but when I try to compile a simple program I get errors that I cannot understand.
The code is very short and simple:
But when I compile it I get errors:
If you want to try for yourself I wrote it on Wokwi and this is the link:
https://wokwi.com/projects/403318967701557249
I get exactly the same errors on my ESP32-WROOM-32, but I used Wokwi to give anyone interested an easy way to try the code for themselves.
The error suggests that the header file is not defining the struct sockaddr_in but I had a look through the source code here:
https://github.com/espressif/esp-lwip/b ... /sockets.h
and it looks to me as if the struct is defined. I note the definition is wrapped in a #if statement:
and I tried adding above the #include but that made no difference.
If anyone can figure out what is causing the errors I would be most grateful. I have been bashing my head on this for a day now without getting anywhere.
The code is very short and simple:
Code: Select all
#include "lwip/sockets.h"
void setup() {
Serial.begin(115200);
int sock_srv = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct sockaddr addr_in;
memset(&addr_in, 0, sizeof(addr_in));
struct sockadder_in* p_addr_in = (struct sockadder_in*) &addr_in;
p_addr_in->sin_family = PF_INET;
p_addr_in->sin_port = htons(23);
p_addr_in->sin_addr.s_addr = INADDR_ANY;
bind(sock_srv, &addr_in, sizeof(addr_in));
listen(sock_srv, 1);
}
void loop() {
delay(100);
}
Code: Select all
sketch.ino:12:12: error: invalid use of incomplete type 'struct setup()::sockadder_in'
12 | p_addr_in->sin_family = PF_INET;
| ^~
sketch.ino:11:10: note: forward declaration of 'struct setup()::sockadder_in'
11 | struct sockadder_in* p_addr_in = (struct sockadder_in*) &addr_in;
| ^~~~~~~~~~~~
sketch.ino:13:12: error: invalid use of incomplete type 'struct setup()::sockadder_in'
13 | p_addr_in->sin_port = htons(23);
| ^~
sketch.ino:11:10: note: forward declaration of 'struct setup()::sockadder_in'
11 | struct sockadder_in* p_addr_in = (struct sockadder_in*) &addr_in;
| ^~~~~~~~~~~~
sketch.ino:14:12: error: invalid use of incomplete type 'struct setup()::sockadder_in'
14 | p_addr_in->sin_addr.s_addr = INADDR_ANY;
| ^~
sketch.ino:11:10: note: forward declaration of 'struct setup()::sockadder_in'
11 | struct sockadder_in* p_addr_in = (struct sockadder_in*) &addr_in;
| ^~~~~~~~~~~~
https://wokwi.com/projects/403318967701557249
I get exactly the same errors on my ESP32-WROOM-32, but I used Wokwi to give anyone interested an easy way to try the code for themselves.
The error suggests that the header file is not defining the struct sockaddr_in but I had a look through the source code here:
https://github.com/espressif/esp-lwip/b ... /sockets.h
and it looks to me as if the struct is defined. I note the definition is wrapped in a #if statement:
Code: Select all
#if LWIP_IPV4
/* members are in network byte order */
struct sockaddr_in {
u8_t sin_len;
sa_family_t sin_family;
in_port_t sin_port;
struct in_addr sin_addr;
#define SIN_ZERO_LEN 8
char sin_zero[SIN_ZERO_LEN];
};
#endif /* LWIP_IPV4 */
Code: Select all
#define LWIP_IPV4 1
Code: Select all
#include "lwip/sockets.h"
If anyone can figure out what is causing the errors I would be most grateful. I have been bashing my head on this for a day now without getting anywhere.