I've attached a simpler version of the server file removing the logic for the
incoming requests so you can take a better look. How should I make the API call
to get the device IP?
________________________________
De: Petro Karashchenko <[email protected]>
Enviado: quinta-feira, 21 de setembro de 2023 09:00
Para: [email protected] <[email protected]>
Assunto: Re: different IP than expected
Yeah,
But what is in between
[image.png]
and
[image.png]
???
Do you have any API call to get the IP? Because I read that like "int a = 10;
printf("%d", a);"
If there is no API call to update "address" then you simply will print
"INADDR_ANY" value.
чт, 21 вер. 2023 р. о 14:42 MIGUEL ALEXANDRE WISINTAINER
<[email protected]<mailto:[email protected]>> пише:
I mean, ifconfig, and capture the ip…
Enviado do meu iPhone
Em 21 de set. de 2023, à(s) 08:24, Gustavo Soares
<[email protected]<mailto:[email protected]>>
escreveu:
Hi, Petro!
I use this struct:
[image.png]
and this where I use the inet_ntoa:
[image.png]
________________________________
De: Petro Karashchenko
<[email protected]<mailto:[email protected]>>
Enviado: quinta-feira, 21 de setembro de 2023 08:16
Para: [email protected]<mailto:[email protected]>
<[email protected]<mailto:[email protected]>>
Assunto: Re: different IP than expected
Hi,
But what do you pass to "inet_ntoa"? Maybe you get the IP of your PC or a
default router IP and try to print it?
Best regards,
Petro
чт, 21 вер. 2023 р. о 14:06 Gustavo Soares
<[email protected]<mailto:[email protected]>>
пише:
I don't know if I can reproduce this in another app, but my question is why
does the server listen to requests in one IP adress (ending with 15) and when I
retrieve its IP it shows a different one? About the ifconfig command, this is
the output:
[X]
the server response to the GET request (the request was sent to the configured
IP using ifconfig):
[X]
the IP address the server returns with the inet_ntoa() method:
[X]
________________________________
From: Petro Karashchenko
<[email protected]<mailto:[email protected]>>
Sent: Thursday, September 21, 2023 6:59:01 AM
To: [email protected]<mailto:[email protected]>
<[email protected]<mailto:[email protected]>>
Subject: Re: different IP than expected
Hi,
"INADDR_ANY" definitely does not match "listens to the defined IP" statement. I
do not understand exactly what you are trying to do, but it would be good to
know what is the output of "ifconfig" running from NSH.
If you do not want to share code details, maybe you can check how your
application behaves on Linux and compare to what behavior you observe with
NuttX.
Can this somehow be reproduced with one of the apps available in the NuttX repo?
Best regards,
Petro
чт, 21 вер. 2023 р. о 03:09 Gustavo Soares
<[email protected]<mailto:[email protected]>>
пише:
Hello everyone!
I configured NuttX to a certain IP address and I have a webserver which
listens to the defined IP:
[X]
[X]
The HTML file above has a variable that contains the server IP address, but it
is a different one:
[X]
the server replaces the text SERVER_IP_ADDRESS to it's actual IP and then sends
the file to the client. This is the method I used to get the IP address:
[X]
Why is it a different IP? The server is listening to one but this method
returns another one.
int main(int argc, char *argv[])
{
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[2048] = {0};
char response[4096] = {0};
const char *file = "/data/client.html";
bool executing = false;
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
printf("waiting for requests at %s:%d...\n", inet_ntoa(address.sin_addr),
PORT);
while (1)
{
// web server logic for requests
}
return 0;
}