创建客户端连接日志 - 如何更改上次连接的代码
这是在互联网上找到的标准例程 获取已连接客户端的 IP/MAC 地址。有用。 我不明白的是如何只获取最后一个访问的 MAC 以保留访问日志。 我不想每次有新连接时都重复每次连接。 我找不到 STAILQ_NEXT 上的文档,所以我不知道它在做什么。 帮助表示赞赏。
unsigned char number_client;
struct station_info *stat_info;
// struct ip_addr *IPaddress;
struct ip4_addr *IPaddress;
IPAddress address;
int i=1;
number_client= wifi_softap_get_station_num();
stat_info = wifi_softap_get_station_info();
Serial.print(" Total Connected Clients are = ");
Serial.println(number_client);
while (stat_info != NULL) {
IPaddress = &stat_info->ip;
address = IPaddress->addr;
Serial.print(stat_info->bssid[0],HEX);Serial.print(':');
Serial.print(stat_info->bssid[1],HEX);Serial.print(':');
Serial.print(stat_info->bssid[2],HEX);Serial.print(':');
Serial.print(stat_info->bssid[3],HEX);Serial.print(':');
Serial.print(stat_info->bssid[4],HEX);Serial.print(':');
Serial.print(stat_info->bssid[5],HEX);Serial.print(' ');
stat_info = STAILQ_NEXT(stat_info, next);
i++;
Serial.println();
}
delay(500);
}```
================================
Thanks Majenko. This gives me the MAC, but if I got the rest of the code correct, this is always giving me the last MAC to connect, rather than the last MAC to access the page. If there are 3 connected A, B, C and A refreshes, the log shows C. Not sure if the list if FIFO or LIFO or FILO or ??? or there's something else I am missing. Maybe I am looking at the wrong variable or ???
```void clientMAC(){
struct station_info *scan = wifi_softap_get_station_info();
byte MAClast[5];
if (scan != NULL) { // Check there is at least one entry
while (STAILQ_NEXT(scan, next) != NULL) {
scan = STAILQ_NEXT(scan, next);
}
// scan now points to the last entry
Serial.print(scan->bssid[0],HEX);Serial.print(':');
Serial.print(scan->bssid[1],HEX);Serial.print(':');
Serial.print(scan->bssid[2],HEX);Serial.print(':');
Serial.print(scan->bssid[3],HEX);Serial.print(':');
Serial.print(scan->bssid[4],HEX);Serial.print(':');
Serial.print(scan->bssid[5],HEX);Serial.print(' ');
}else { // We have no entries
}
}
```
This is the standard routine found all over the internet for
obtaining the IP/MAC address of an connected client. It works.
What I can't figure out is how to get only the last MAC to access to keep an access log.
I don't want a repeat of every connect, each time there's a new connect.
I can't find docs on STAILQ_NEXT, so I don't know what it is doing.
Help appreciated.
unsigned char number_client;
struct station_info *stat_info;
// struct ip_addr *IPaddress;
struct ip4_addr *IPaddress;
IPAddress address;
int i=1;
number_client= wifi_softap_get_station_num();
stat_info = wifi_softap_get_station_info();
Serial.print(" Total Connected Clients are = ");
Serial.println(number_client);
while (stat_info != NULL) {
IPaddress = &stat_info->ip;
address = IPaddress->addr;
Serial.print(stat_info->bssid[0],HEX);Serial.print(':');
Serial.print(stat_info->bssid[1],HEX);Serial.print(':');
Serial.print(stat_info->bssid[2],HEX);Serial.print(':');
Serial.print(stat_info->bssid[3],HEX);Serial.print(':');
Serial.print(stat_info->bssid[4],HEX);Serial.print(':');
Serial.print(stat_info->bssid[5],HEX);Serial.print(' ');
stat_info = STAILQ_NEXT(stat_info, next);
i++;
Serial.println();
}
delay(500);
}```
================================
Thanks Majenko. This gives me the MAC, but if I got the rest of the code correct, this is always giving me the last MAC to connect, rather than the last MAC to access the page. If there are 3 connected A, B, C and A refreshes, the log shows C. Not sure if the list if FIFO or LIFO or FILO or ??? or there's something else I am missing. Maybe I am looking at the wrong variable or ???
```void clientMAC(){
struct station_info *scan = wifi_softap_get_station_info();
byte MAClast[5];
if (scan != NULL) { // Check there is at least one entry
while (STAILQ_NEXT(scan, next) != NULL) {
scan = STAILQ_NEXT(scan, next);
}
// scan now points to the last entry
Serial.print(scan->bssid[0],HEX);Serial.print(':');
Serial.print(scan->bssid[1],HEX);Serial.print(':');
Serial.print(scan->bssid[2],HEX);Serial.print(':');
Serial.print(scan->bssid[3],HEX);Serial.print(':');
Serial.print(scan->bssid[4],HEX);Serial.print(':');
Serial.print(scan->bssid[5],HEX);Serial.print(' ');
}else { // We have no entries
}
}
```
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
stat_info
是一个struct station_info *
类型的简单链表,具有由 SDK 强加的预定义结构。以下是完整的实现链:STAILQ_ENTRY(station_info) next
宏扩展为:因此,您有一个基于
stat_info->next.stqe_next
的链表。宏
STAILQ_NEXT(stat_info, next)
只是将其包装在一些抽象中,以使迭代更容易。它扩展为((stat_info)->next.stqe_next
。要获取链表的最后一个元素,您需要迭代列表,直到
->next.stqe_next
code> 为 null,此时您知道自己位于列表的末尾。stat_info
is a simple linked list of typestruct station_info *
, with a predefined structure imposed by the SDK. Here's the full chain of implementation:The
STAILQ_ENTRY(station_info) next
macro expands to:So you have a linked list that is based around
stat_info->next.stqe_next
.The macro
STAILQ_NEXT(stat_info, next)
just wraps that in a bit of abstraction to make iterating easier. It expands out to((stat_info)->next.stqe_next
.To get the last element of a linked list you need to iterate through the list until
->next.stqe_next
is null, at which point you know you are at the end of the list. For example: