Pinging a Server Port with PHP
As you've correctly noted, the provided PHP script is designed to ping websites by their domain name. To adapt it for pinging specific IP addresses and port numbers, we need to make some adjustments.
The key function you'll need is fsockopen(), which allows you to establish a socket connection to a remote server. By specifying the server's IP address and port number, you can attempt to connect and verify if the server is accepting connections on that specific port.
Here's a revised version of the ping function:
<code class="php">function pingPort($ip, $port, $timeout) { $tB = microtime(true); $fP = fsockopen($ip, $port, $errno, $errstr, $timeout); if (!$fP) { return "down"; } $tA = microtime(true); return round((($tA - $tB) * 1000), 0)." ms"; }</code>
This function takes three arguments:
- $ip: The IP address of the server you want to ping
- $port: The port number you want to ping
- $timeout: The amount of time (in seconds) to wait for a response before giving up
If the connection can be established, the function will return the round-trip time in milliseconds. Otherwise, it will return "down" to indicate that the port is not reachable.
To use the function, you can simply call it like this:
<code class="php">$ip = "192.168.1.100"; $port = 80; $timeout = 10; $ping = pingPort($ip, $port, $timeout); if ($ping == "down") { echo "Port $port is not reachable on $ip."; } else { echo "Ping to $ip:$port: $ping ms"; }</code>
This script will display the ping time if the port is reachable, or it will output an error message if the connection attempt fails.
以上是如何使用 PHP Ping 伺服器連接埠:逐步指南的詳細內容。更多資訊請關注PHP中文網其他相關文章!

長URL(通常用關鍵字和跟踪參數都混亂)可以阻止訪問者。 URL縮短腳本提供了解決方案,創建了簡潔的鏈接,非常適合社交媒體和其他平台。 這些腳本對於單個網站很有價值

在Facebook在2012年通過Facebook備受矚目的收購之後,Instagram採用了兩套API供第三方使用。這些是Instagram Graph API和Instagram Basic Display API。作為開發人員建立一個需要信息的應用程序

Laravel使用其直觀的閃存方法簡化了處理臨時會話數據。這非常適合在您的應用程序中顯示簡短的消息,警報或通知。 默認情況下,數據僅針對後續請求: $請求 -

這是有關用Laravel後端構建React應用程序的系列的第二個也是最後一部分。在該系列的第一部分中,我們使用Laravel為基本的產品上市應用程序創建了一個RESTFUL API。在本教程中,我們將成為開發人員

Laravel 提供简洁的 HTTP 响应模拟语法,简化了 HTTP 交互测试。这种方法显著减少了代码冗余,同时使您的测试模拟更直观。 基本实现提供了多种响应类型快捷方式: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

PHP客戶端URL(curl)擴展是開發人員的強大工具,可以與遠程服務器和REST API無縫交互。通過利用Libcurl(備受尊敬的多協議文件傳輸庫),PHP curl促進了有效的執行

您是否想為客戶最緊迫的問題提供實時的即時解決方案? 實時聊天使您可以與客戶進行實時對話,並立即解決他們的問題。它允許您為您的自定義提供更快的服務

2025年的PHP景觀調查調查了當前的PHP發展趨勢。 它探討了框架用法,部署方法和挑戰,旨在為開發人員和企業提供見解。 該調查預計現代PHP Versio的增長


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3漢化版
中文版,非常好用

記事本++7.3.1
好用且免費的程式碼編輯器

Dreamweaver Mac版
視覺化網頁開發工具