Method for detecting network connection state of embedded device and embedded device

By using the domain name resolution function in an embedded device to obtain the target host address and attempting TCP connection in non-blocking mode, the problems of weak universality and low efficiency of network connection status detection in the prior art are solved, and more realistic connectivity verification and deeper transport layer verification are achieved.

CN119854174APending Publication Date: 2025-04-18SHENZHEN XINRUISHI TECH CO LTD
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
CN202510331237.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-03-20
Publication Date
2025-04-18

AI Technical Summary

Technical Problem

The existing embedded device network connection status detection technology has weak versatility, is difficult to achieve lightweight data processing, cannot provide more realistic connectivity verification, low connection testing efficiency and poor stability.

Method used

The domain name calls the address resolution function to obtain the IPv4 or IPv6 address information available to the target host, create a socket for each address and set it to non-blocking mode, perform TCP connection attempts on all available address information of the target host in non-blocking mode, and monitor the writable state, error state and timeout state of the socket through the select function to determine whether the connection is successful.

Benefits of technology

It realizes the universality of network connection status detection of embedded devices, avoids data exchange in specific protocol layers, and realizes lightweight data processing. It not only relies on routing configuration, but also actually tries to establish connections, providing more realistic connectivity verification, suitable for a variety of network services, and ensures high efficiency and reliability through non-blocking and timeout mechanisms.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119854174A_ABST
    Figure CN119854174A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of embedded devices, network connection and the like, and provides a method for detecting the network connection state of an embedded device and the embedded device. An address resolution function is called through a domain name to obtain available IPv4 or IPv6 address information of a target host; the method comprises the steps of obtaining available address information of a target host, creating a socket for each piece of obtained available address information, setting the socket to be in a non-blocking mode, performing TCP connection attempt on target ports corresponding to all the available address information of the target host in the non-blocking mode, monitoring a writable state, an error state and an overtime state of the socket through a select function so as to judge whether connection succeeds or not, and if yes, executing the TCP connection attempt on the target ports corresponding to all the available address information of the target host. After TCP connection attempts of addresses in all available address information are completed, all available address information is released, sockets are closed, and a connection detection result is returned according to whether successfully connected addresses exist or not, so that detection universality and lightweight data processing are realized, the method is suitable for various network services, and high efficiency and reliability are ensured.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical fields of embedded devices, network connections, and in particular to a method for detecting the network connection state of an embedded device and an embedded device. Background Art

[0002] In the prior art, the detection methods of the network connection status of embedded devices mainly include Ping test, DNS resolution, HTTP / HTTPS request and checking the default gateway and other technical solutions. Ping test mainly uses ICMP protocol to send Echo request to the target IP address and waits for response. This method can be used to detect whether it is possible to communicate with a specific IP address, such as whether it can connect to a router or an external embedded device. DNS resolution mainly attempts to resolve a known domain name. If the resolution is successful, it means that the DNS service is available, which usually means a valid network connection. This method can be used to detect whether there is the ability to access resources on the Internet. HTTP / HTTPS request mainly attempts to send a GET request to a reliable external embedded device such as Google or other public APIs via HTTP or HTTPS. This method not only detects network connectivity, but also verifies whether there is normal Internet access. Checking the default gateway mainly reads the system routing table to see if there is a default gateway (i.e., the path to the external network). This method can quickly determine whether the device is configured with a route to the external network. However, in the above existing solutions, the Ping test has the problem that some networks may disable ICMP echo requests, resulting in misjudgment; DNS resolution depends on the availability of DNS embedded devices; HTTP / HTTPS requests require more resources and time to complete the test; and checking the default gateway can only detect the existence of routing configuration but cannot guarantee actual connectivity.

[0003] In summary, the existing embedded device network connection status detection technology has the following technical problems: weak versatility, involving specific protocol layer data exchange and difficulty in achieving lightweight data processing, unable to provide more realistic connectivity verification, low connection test efficiency, poor stability, etc. Summary of the invention

[0004] In view of the deficiencies in the above-mentioned prior art, the present invention provides a method for detecting the network connection status of an embedded device and an embedded device, so as to enhance the versatility of the embedded device network connection status detection, avoid involving specific protocol layer data exchange, realize lightweight data processing, not only rely on routing configuration, but also actually attempt to establish a connection, provide more realistic connectivity verification, provide more in-depth transport layer verification, be applicable to a variety of network services, and ensure high efficiency and reliability through non-blocking and timeout mechanisms.

[0005] In a first aspect, the present invention provides a method for detecting a network connection status of an embedded device, comprising: Calling an address resolution function through a domain name to obtain available IPv4 or IPv6 address information of a target host, creating a socket for each available address information obtained and setting the socket to a non-blocking mode; In non-blocking mode, a TCP connection attempt is made to the target port corresponding to all available address information of the target host, and the writable state, error state and timeout state of the socket are monitored through the select function to determine whether the connection is successful; After completing the TCP connection attempts of the addresses in all the available address information, all the available address information is released and the socket is closed, and a connection detection result is returned according to whether there is an address that is successfully connected.

[0006] In a second aspect, the present invention provides an embedded device, wherein the embedded device applies a method for detecting a network connection status of an embedded device to detect a network connection status.

[0007] Compared with the prior art, the present invention has the following beneficial effects: The present invention provides a method for detecting the network connection status of an embedded device and an embedded device. The method calls an address resolution function through a domain name to obtain IPv4 or IPv6 address information available to a target host, creates a socket for each available address information obtained and sets the socket to a non-blocking mode, performs a TCP connection attempt on a target port corresponding to all available address information of the target host in the non-blocking mode, monitors the writable state, error state and timeout state of the socket through a select function to determine whether the connection is successful, releases all available address information and closes the socket after completing the TCP connection attempt of the address in all available address information, and returns a connection detection result according to whether there is an address that is successfully connected, thereby enhancing the versatility of the embedded device network connection status detection, avoiding the involvement of specific protocol layer data exchange, and realizing lightweight data processing. The method not only relies on routing configuration but also actually attempts to establish a connection, provides more realistic connectivity verification, provides more in-depth transport layer verification, is applicable to a variety of network services, and ensures high efficiency and reliability through non-blocking and timeout mechanisms. BRIEF DESCRIPTION OF THE DRAWINGS

[0008] The drawings described herein are used to provide a further understanding of the present invention and constitute a part of the present invention. The exemplary embodiments of the present invention and their description are used to explain the present invention and do not constitute an improper limitation of the present invention. Some specific embodiments of the present invention will be described in detail in an exemplary and non-restrictive manner with reference to the drawings. The same reference numerals in the drawings indicate the same or similar components or parts. It should be understood by those skilled in the art that these drawings are not necessarily drawn to scale. In the drawings: Figure 1 It is a flow chart of a method for detecting a network connection status of an embedded device according to an embodiment of the present invention; Figure 2 It is a schematic diagram of a system architecture of an embedded device according to an embodiment of the present invention. DETAILED DESCRIPTION

[0009] In order to enable those skilled in the art to better understand the scheme of the present invention, the technical scheme in the embodiment of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiment of the present invention. Obviously, the described embodiment is only an embodiment of a part of the present invention, not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work should fall within the scope of protection of the present invention.

[0010] Embodiment 1 See also Figure 1-Figure 2 This embodiment provides a method for detecting the network connection status of an embedded device, including step S101, step S102 and step S103.

[0011] S101, calling an address resolution function through a domain name to obtain available IPv4 or IPv6 address information of a target host, creating a socket for each available address information obtained and setting the socket to a non-blocking mode; S102, in non-blocking mode, performing a TCP connection attempt on the target port corresponding to all available address information of the target host, and monitoring the writable state, error state and timeout state of the socket through the select function to determine whether the connection is successful; S103: After completing the TCP connection attempts of the addresses in all the available address information, release all the available address information and close the socket, and return the connection detection result according to whether there is an address that is successfully connected.

[0012] It should be noted that in step S101, the address resolution function is called by the domain name to obtain the available IPv4 or IPv6 address information of the target host, and a socket is created for each available address information obtained and the socket is set to non-blocking mode. Wherein, the domain name is a text form name used to identify and locate the host in the network. The address resolution function is a function provided by the system (such as getaddrinfo()), through which the domain name can be converted into an IPv4 or IPv6 address list. IPv4 and IPv6 represent the fourth and sixth versions of the Internet Protocol, respectively. This step is compatible with a variety of network environments and is no longer limited to a single protocol stack, thereby improving the versatility of the detection method. In addition, in this embodiment, a socket is created for each available address information obtained, and a socket is a network communication endpoint provided by the operating system for sending and receiving network data. Preferably, a TCP stream socket can be used to facilitate more realistic connectivity verification at the transport layer. When the creation fails or is unavailable, the next address can be tried to avoid detection interruption due to anomalies of a single address. In addition, in this embodiment, the socket is set to non-blocking mode. Non-blocking mode means that the I / O operation (such as connect) performed on the socket will not cause the calling thread to pause for a long time, but will immediately return to the current state (which may be in progress). Preferably, the socket can be set to non-blocking by calling a system call similar to fcntl to reduce the detection efficiency that may be reduced due to blocking waiting. It can be understood that in this embodiment, the address resolution function is called by the domain name to obtain the IPv4 or IPv6 address information available to the target host, a socket is created for each available address information obtained, and the socket is set to non-blocking mode, which can avoid problems similar to Ping relying on ICMP and being disabled by network devices. In this embodiment, it not only depends on whether there is a routing configuration, but also actually tries to connect to the network to obtain a more realistic connectivity judgment. In addition, the non-blocking method can be used in conjunction with subsequent timeout control to improve detection efficiency and response speed.

[0013] It should be noted that in step S102, a TCP connection attempt is made to the target port corresponding to all available address information of the target host in non-blocking mode, and the writable state, error state and timeout state of the socket are monitored through the select function to determine whether the connection is successful. First, a connection (connect) is initiated in non-blocking mode. Connect is a system call used to initiate a TCP connection request to a remote host. In non-blocking mode, when connect returns -1 and errno is displayed as in progress (such as EINPROGRESS), it means that the connection has not been completed, and it is necessary to use other mechanisms to determine whether it can be connected. In addition, the connection status is monitored by the select function. Select is a system call for simultaneously monitoring the status changes of multiple file descriptors (including sockets). For example, the writable state, error state (error), and timeout state (timeout) of the socket are monitored to determine whether the connection is completed. For the writable state (writable), if the socket is triggered by a writable event, it means that the underlying connection is successfully established or an error occurs, and it is necessary to further obtain the connection result through getsockopt and other methods. For the error state (error), if an error is detected in the socket, it means that the connection has failed. For the timeout state (timeout), if no writable or error event is triggered within the set time, it means that the connection may have timed out or the network is congested. At this time, it can be regarded as a connection failure and the next address is tried or the detection is ended. It can be understood that in this embodiment, by actually initiating a TCP connection attempt and monitoring whether the connection is completed, the network connectivity is truly verified from the transport layer, which is different from relying solely on whether the DNS is resolvable or whether the default gateway exists. In this embodiment, the non-blocking mode is combined with the timeout mechanism to reduce the waiting time and system burden, and the test efficiency is higher. It can avoid the heavy resource consumption like HTTP / HTTPS requests and the limitations of relying solely on the success or failure of DNS.

[0014] It should be noted that in step S103, after completing the TCP connection attempt of the address in all available address information, all available address information is released and the socket is closed, and the connection detection result is returned according to whether there is a successfully connected address. First, all available address information (freeaddrinfo, etc.) is released. The address resolution function can dynamically allocate a linked list or array, which needs to be released in time after use to avoid memory leaks. This release operation can ensure the effective management of system resources. Secondly, close the socket. If a certain address has been successfully connected, the socket can be directly closed and a successful result can be returned; if all addresses fail, the sockets should be closed one by one and a failed result should be returned. In this embodiment, by closing the socket in time, the connection is prevented from occupying excess system resources. In addition, the result is returned according to whether there is a successfully connected address. When any address successfully completes the connection, it means that the network is indeed reachable, so that a successful connection is judged; if all fail, a connection failure is returned. It can be understood that in this embodiment, the network is only determined to be available if a connection has been truly established, which further improves the accuracy of the detection. It does not need to transmit application layer data, and only needs to complete the TCP handshake to determine connectivity, reducing bandwidth and time consumption, and achieving lightweight data processing. Compared with common Ping tests, DNS tests, or HTTP / HTTPS requests, it can better reflect the actual transport layer connectivity.

[0015] In summary, this embodiment supports both IPv4 and IPv6, and only requires the target domain name to work. It is not limited to whether ICMP (Ping) can be used, nor does it rely on the correctness of the configuration of the default gateway, and does not need to be bound to the data exchange of a specific protocol layer. There is no need to load more application layer overhead like HTTP / HTTPS requests. Only the process of TCP three-way handshake is completed to determine the connectivity, which is more efficient. Instead of just looking at whether the routing table or DNS can be resolved, the transport layer is verified through the actual socket connection action, which can truly reflect the network availability status. Through non-blocking I / O + select listening for writable events + timeout judgment, long-term blocking of a single address can be avoided, and good real-time and stability can be maintained when the network is busy or the device is running multi-tasking. Since the underlying layer is based on TCP sockets, whether the target is HTTP service, MQTT service, database service, etc., the network connectivity can be obtained through this detection method, which has a certain degree of general adaptability.

[0016] In some preferred embodiments, when calling the address resolution function through the domain name, setting the query parameter of the addrinfo structure allows the simultaneous acquisition of IPv4 and IPv6 address information to be compatible with multiple network protocol environments. It should be noted that in this embodiment, by setting the query parameter of the addrinfo structure, it is allowed to simultaneously acquire IPv4 and IPv6 address information, that is, when calling the address resolution function (such as getaddrinfo()), through appropriate flags or configuration parameters, the system is instructed to return available addresses of IPv4 and IPv6 types at the same time. In this way, the same set of detection methods can be used in networks that support IPv4 and IPv6, thereby avoiding the complexity caused by different processing for IPv4 or IPv6 alone, and unified network connectivity detection can be performed on embedded devices of different network formats. In this embodiment, the addrinfo structure is a structure that stores host address information returned by the system call (such as getaddrinfo()), including information such as IP address, port, and protocol family. IPv4 / IPv6 refer to the fourth and sixth versions of the Internet Protocol, respectively, representing different network address formats.

[0017] In some preferred embodiments, when creating a socket for each available address information, a stream socket is specified, and when the creation fails, the next available address information is continued to be tried. It should be noted that in this embodiment, a stream socket (TCP) is used when creating a socket, rather than other types (such as UDP or raw sockets), and it is stipulated that if a failure occurs when creating a socket for a certain address, the subsequent addresses are continued to be tried, thereby improving fault tolerance. It can be understood that the stream socket (TCP) performs connection-oriented communication at the transport layer, so that the detection method can truly simulate and verify the availability of the transport layer. If there is a problem with a single address information (such as a parsing exception or the system does not support it), the subsequent addresses are continued to be tried, thereby enhancing the robustness of the method. A stream socket refers to a socket based on the TCP protocol, which can provide ordered, reliable, and bidirectional byte stream transmission.

[0018] In some preferred embodiments, when the socket is set to non-blocking mode, it includes: setting the socket to non-blocking mode by using fcntl or an equivalent system call to avoid detection delays caused by blocking connections. It should be noted that in this embodiment, when setting the socket to non-blocking mode, the socket can be set to non-blocking mode by using fcntl or an equivalent system call. Among them, the non-blocking mode allows subsequent connection requests to return quickly when the underlying network has not yet been completed, so that the detection process is not delayed for a long time by a single connection operation, which is conducive to quickly switching to the next detection operation in a multi-connection or multi-address scenario. In addition, fcntl is a system call used to operate file descriptor features in the POSIX system. In this embodiment, fcntl is used to switch the socket from blocking mode to non-blocking mode.

[0019] In some preferred embodiments, when a connection request is initiated for the target port corresponding to the available address information, connect is called to try to establish a connection. When connect returns -1 and errno is EINPROGRESS, subsequent connection detection based on non-blocking mode will continue to be performed. It should be noted that when the typical return value -1 encountered when calling connect under non-blocking mode and errno is EINPROGRESS, it means that the connection is still in progress, rather than failing immediately, and needs to be detected by other methods (such as select) later. It is understandable that if connect has not completed the handshake, it will immediately return without blocking the thread. It will not be regarded as a failure because of the return value of -1, but the ongoing state will be distinguished and subsequent detection logic will be executed. Errno refers to the system error code, which is used to indicate the reason for the failure of the system call; EINPROGRESS means that the current operation is still in progress and has not been completed.

[0020] In some preferred embodiments, when the writable state of the socket is monitored by the select function, the timeout period is dynamically set based on the device requirements or network conditions, so as to wait longer in an unstable network environment or speed up the detection switching in a high-concurrency environment. It should be noted that when the select function is used to detect the socket status, the timeout period can be set dynamically. The waiting time is adjusted automatically with the difference in network environment (such as delay, packet loss rate) or different device performance requirements. It is understandable that in this embodiment, the timeout size can be determined based on the actual network quality or the timeliness requirement for detection. In the case of network fluctuations or high concurrency scenarios, frequent timeouts or long waits can be avoided through a reasonable timeout strategy.

[0021] In some preferred embodiments, when monitoring the writable state of the socket, the FD_ISSET function is used to determine whether the socket is in the writable set. If the socket is detected to be writable, the connection error state is further obtained. It should be noted that in this embodiment, the FD_ISSET function is used to determine whether the socket is in the writable set (writefds). If it is writable, it indicates that the connection may have been completed and further inspection is required to determine whether an error has occurred. It can be understood that in this embodiment, success is not directly determined by writability, but getsockopt or similar methods can be used to detect whether there is an error in the socket, thereby avoiding misjudgment, thereby providing a standard method to determine the difference between a writable event and a truly successful connection. Among them, FD_ISSET is a macro or function used in conjunction with select to check whether a file descriptor is in a specified readable / writable / abnormal set.

[0022] In some preferred embodiments, obtaining the error status of the socket is completed by calling the getsockopt function. When the error status is 0, the connection is determined to be successful, otherwise the connection is determined to fail and the subsequent address information is continued to be tried. It should be noted that in this embodiment, when it is detected that the socket is writable, it is necessary to obtain a specific error code (SO_ERROR) through the getsockopt function. If the error code is 0 (i.e., no error), the connection is successful, otherwise the connection fails. It can be understood that it is not enough to determine that the connection is successful based on the writable socket alone. By reading the error status, it can be confirmed whether the TCP handshake is truly completed. If it fails, continue to try other addresses to improve the success rate. Among them, getsockopt refers to a system call function used to obtain or set option information associated with the socket. It is used here to obtain the SO_ERROR option to determine the connection status.

[0023] In some preferred embodiments, the socket is immediately closed after the connection is successful and the TCP connection attempts of the subsequent addresses are terminated to reduce resource usage and improve detection efficiency. It should be noted that in this embodiment, if a certain address has been successfully connected, the socket is immediately closed, and there is no need to continue to attempt to connect to the remaining addresses, avoiding the establishment of unnecessary connections and occupying system resources. It is understandable that as long as one address can be connected, indicating that the network connection is available, the detection can be quickly terminated to avoid repeated or unnecessary connection requests on multiple possible addresses.

[0024] In some further preferred embodiments, after completing the TCP connection attempts for the addresses in all available address information, if no connection is successfully established, the result of the detection failure is returned and the corresponding error information is output so as to execute subsequent fault diagnosis or switching strategies. It should be noted that in this embodiment, when all addresses are tried and still cannot be connected, the connection detection failure is returned, and an error message is output, and further post-failure processing suggestions (such as fault diagnosis or network switching) are given. It is understandable that fault log records or prompts can be made on the device side to facilitate subsequent maintenance or automatic switching to a backup network, ensuring that both success and failure scenarios have clear subsequent processing.

[0025] In some further preferred embodiments, the detection method uniformly releases the memory resources allocated for address resolution by calling freeaddrinfo after completing the detection, and is further applied to various network protocol scenarios that require connection verification at the transport layer. It should be noted that in this embodiment, calling freeaddrinfo uniformly releases the memory allocated for address resolution to ensure complete resource recovery, which is not only applicable to certain specific ports or protocols, but also can be extended to various network service scenarios based on the transport layer (TCP). It can be understood that in this embodiment, the dynamically allocated address information is released in time after use to ensure full utilization of limited resources of embedded devices. Moreover, since the detection method in this embodiment can rely on TCP layer verification, it is not limited to specific protocols such as HTTP or DNS, and has a wider applicability. Among them, freeaddrinfo is a system function, which is used in conjunction with getaddrinfo to release the previously allocated address information list.

[0026] Embodiment 2 See also Figure 1-Figure 2The present embodiment provides an embedded device, which applies the method for detecting the network connection status of the embedded device in the above embodiment to detect the network connection status, calls the address resolution function through the domain name to obtain the available IPv4 or IPv6 address information of the target host, creates a socket for each available address information obtained and sets the socket to non-blocking mode, performs a TCP connection attempt on the target port corresponding to all the available address information of the target host in the non-blocking mode, monitors the writable state, error state and timeout state of the socket through the select function to determine whether the connection is successful, releases all the available address information and closes the socket after completing the TCP connection attempt of the address in all the available address information, and returns the connection detection result according to whether there is an address that is successfully connected, thereby enhancing the versatility of the embedded device network connection status detection, avoiding the involvement of specific protocol layer data exchange, and realizing lightweight data processing, not only relying on routing configuration, but also actually trying to establish a connection, providing more realistic connectivity verification, providing more in-depth transport layer verification, being applicable to a variety of network services, and ensuring high efficiency and reliability through non-blocking and timeout mechanisms.

[0027] It is understood that an embedded device may be an information processing device including a memory, a processor and a network interface that are interconnected through a system bus. A person skilled in the art may understand that an embedded device here is a device that can automatically perform numerical calculations and / or information processing according to pre-set or stored instructions, and its hardware includes but is not limited to a microprocessor, an application specific integrated circuit (ASIC), a field programmable gate array (FPGA), a digital processor (DSP), an embedded device, etc. The memory includes at least one type of readable storage medium, and the readable storage medium includes a flash memory, a multimedia card, a card-type memory (e.g., SD or DX memory, etc.), a random access memory (RAM), a static random access memory (SRAM), a read-only memory (ROM), an electrically erasable programmable read-only memory (EEPROM), a programmable read-only memory (PROM), a magnetic memory, etc. In some embodiments, the memory may be an internal storage unit of an embedded device, such as the memory of the embedded device. In other embodiments, the memory may also be an external storage device of the embedded device, such as a smart media card (SMC), a secure digital (SD) card, a flash card, etc. equipped on the embedded device.

[0028] The above embodiments are only preferred specific implementation modes of the present invention, and the protection scope of the present invention is not limited thereto. Any changes or substitutions that can be easily thought of by any technician familiar with the technical field within the technical scope disclosed by the present invention should be covered within the protection scope of the present invention. The protection scope of the present invention should be based on the protection scope of the claims.

Claims

1. A method for detecting the network connection status of an embedded device, characterized in that: include: Calling an address resolution function through a domain name to obtain available IPv4 or IPv6 address information of a target host, creating a socket for each available address information obtained and setting the socket to a non-blocking mode; In non-blocking mode, a TCP connection attempt is made to the target port corresponding to all available address information of the target host, and the writable state, error state and timeout state of the socket are monitored through the select function to determine whether the connection is successful; After completing the TCP connection attempts of the addresses in all the available address information, all the available address information is released and the socket is closed, and a connection detection result is returned according to whether there is an address that is successfully connected.

2. The method for detecting the network connection status of an embedded device according to claim 1, characterized in that: When calling the address resolution function through the domain name, setting the query parameters of the addrinfo structure allows both IPv4 and IPv6 address information to be obtained simultaneously to be compatible with multiple network protocol environments.

3. The method for detecting the network connection status of an embedded device according to claim 1, characterized in that: When creating a socket for each available address information, specify the use of stream sockets, and continue to try the next available address information if the creation fails.

4. The method for detecting the network connection status of an embedded device according to claim 1, characterized in that: When the socket is set to a non-blocking mode, the method includes: setting the socket to a non-blocking mode by using fcntl or an equivalent system call to avoid a detection delay caused by a blocking connection.

5. The method for detecting the network connection status of an embedded device according to claim 1, characterized in that: When a connection request is initiated to the target port corresponding to the available address information, connect is called to try to establish a connection. When connect returns -1 and errno is EINPROGRESS, subsequent connection detection based on non-blocking method will continue to be executed; errno refers to the system error code, which is used to indicate the reason for the failure of the system call; EINPROGRESS means that the current operation is still in progress and has not been completed.

6. The method for detecting the network connection status of an embedded device according to claim 1, characterized in that: When monitoring the writable state of the socket through the select function, the timeout period is dynamically set based on device requirements or network conditions to allow longer waiting in an unstable network environment or to speed up detection switching in a high-concurrency environment.

7. The method for detecting the network connection status of an embedded device according to claim 1, characterized in that: When monitoring the writable state of the socket, the FD_ISSET function is used to determine whether the socket is in the writable set. If it is detected that the socket is writable, the connection error state is further obtained.

8. The method for detecting the network connection status of an embedded device according to claim 1, characterized in that: The error status of the socket is obtained by calling the getsockopt function. When the error status is 0, the connection is determined to be successful, otherwise the connection is determined to have failed and the subsequent address information is continued to be tried.

9. The method for detecting the network connection status of an embedded device according to claim 1, characterized in that: After the connection is successful, the socket is immediately closed and the TCP connection attempt of the subsequent address is ended to reduce resource usage and improve detection efficiency.

10. An embedded device, characterized in that: The embedded device applies the method for detecting the network connection status of the embedded device as described in any one of claims 1-9.

Citation Information

Patent Citations

  • Information acquisition method and device, computer device and storage medium

    CN109286609A

  • Network diagnosis method and system for Android platform, storage medium and electronic equipment

    CN114244758A

  • Network detection method and system based on data development plane suite

    CN116980333A

  • TCP (Transmission Control Protocol) connection information management method and device based on Netlink

    CN119652829A