Status checker for hundreds IP addresses

前端 未结 10 1597
长发绾君心
长发绾君心 2021-01-02 01:52

I wonder how to make a status checker, checking about 500 addresses in a few minutes? (it\'ll check a certain port if its listening).

But I care about the performanc

10条回答
  •  庸人自扰
    2021-01-02 02:19

    The by far most efficient way to do this is to use raw sockets, however this requires administrative privileges and some boilerplate coding. You choose a local port as the source, send a SYN packet to port 80 of each target address, and collect the return packets on the source port. If they have an ACK flag, the port is open for connections. If they have RST or they don't return, the port isn't open/the host is dead.

    Obviously you will need to manually wait for whatever your "timeout" of choice is as this method goes around the OS network stack. This also requires you to construct the TCP and IP headers manually, though, and I doubt it can be done from PHP. It can certainly be done in C, and I believe C# also supports raw sockets (haven't done it in C# myself).

    This is extremely fast and doesn't pollute the system with a bunch of open connections or threads.

提交回复
热议问题