@@ -626,6 +626,61 @@ Sets the IP of the DNS server used to resolve hostnames. Default: resolver1.open
626
626
#### See also
627
627
[ ` net.dns:getdnsserver() ` ] ( #netdnsgetdnsserver )
628
628
629
+
630
+ ### net.ping()
631
+
632
+ This is a function to ping a server. A callback function is called when response is or is not received.
633
+
634
+ The function is disabled by default to enable this functionnality define ` NET_INFO_ENABLE ` macro needs to be defined in ` user_config.h ` .
635
+
636
+ #### Syntax
637
+ ` net.ping(domain, [count], callback) `
638
+
639
+ #### Parameters
640
+ - ` domain ` destination domain or IP address
641
+ - ` count ` number of ping packets to be sent (optional parameter, default value is 4)
642
+ - ` callback(bytes, ipaddr, seqno, rtt) ` callback function which is invoked when response is received where
643
+ - ` bytes ` number of bytes received from destination server (0 means no response)
644
+ - ` ipaddr ` destination serve IP address
645
+ - ` seqno ` ICMP sequence number
646
+ - ` rtt ` round trip time in ms
647
+
648
+ If domain name cannot be resolved callback is invoked with ` bytes ` parameter equal to 0 (i.e. no response) and ` nil ` values for all other parameters.
649
+
650
+ #### Returns
651
+ ` nil `
652
+
653
+ #### Example
654
+ ``` lua
655
+ net .ping (" www.nodemcu.com" , function (b , ip , sq , tm )
656
+ if ip then print ((" %d bytes from %s, icmp_seq=%d time=%dms" ):format (b , ip , sq , tm )) else print (" Invalid IP address" ) end
657
+ end )
658
+ ```
659
+
660
+ Multiple pings can start in short sequence thought if the new ping overlaps with the previous one the first stops receiving answers, i.e.
661
+ ``` lua
662
+ function ping_resp (b , ip , sq , tm )
663
+ print (string.format (" %d bytes from %s, icmp_seq=%d time=%dms" , b , ip , sq , tm ))
664
+ end
665
+
666
+ net .ping (" 8.8.8.8" , 4 , ping_resp )
667
+ tmr .create ():alarm (1000 , tmr .ALARM_SINGLE , function () net .ping (" 8.8.4.4" , 4 , ping_resp ) end )
668
+ ```
669
+ gives
670
+ ```
671
+ 32 bytes from 8.8.8.8, icmp_seq=9 time=14ms
672
+ 32 bytes from 8.8.8.8, icmp_seq=10 time=9ms
673
+ 32 bytes from 8.8.4.4, icmp_seq=11 time=6ms
674
+ 32 bytes from 8.8.4.4, icmp_seq=13 time=12ms
675
+ 0 bytes from 8.8.8.8, icmp_seq=0 time=0ms
676
+ 32 bytes from 8.8.4.4, icmp_seq=15 time=16ms
677
+ 0 bytes from 8.8.8.8, icmp_seq=0 time=0ms
678
+ 32 bytes from 8.8.4.4, icmp_seq=16 time=7ms
679
+ ```
680
+
681
+
682
+
683
+
629
684
# net.cert Module
630
685
631
686
This part gone to the [ TLS] ( tls.md ) module, link kept for backward compatibility.
0 commit comments