Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

php tcpping

作者:傲骨依旧   发布日期:2026-01-06   浏览:139

There is no built-in function in PHP for performing TCP ping (tcpping). However, you can use socket functions in PHP to implement a basic TCP ping functionality. Here's an example:

function tcpping($host, $port, $timeout = 5) {
    $socket = @fsockopen($host, $port, $errno, $errstr, $timeout);

    if ($socket) {
        fclose($socket);
        return true;
    } else {
        return false;
    }
}

// Usage example
$host = 'example.com';
$port = 80;
$timeout = 5;

if (tcpping($host, $port, $timeout)) {
    echo "TCP ping successful!";
} else {
    echo "TCP ping failed!";
}

In this example, the tcpping function takes a host, port, and optional timeout as parameters. It attempts to open a TCP connection to the specified host and port using fsockopen function. If the connection is successful, it returns true, indicating a successful TCP ping. Otherwise, it returns false.

Note that @ is used to suppress any error messages that may occur during the connection attempt. You can remove it if you want to handle the errors explicitly.

上一篇:php实现记录浏览时间

下一篇:php从根目录

大家都在看

php session用法

php 定义常量

phpisset函数

php html转图片

php后端

php爬虫框架

php读取csv文件

php+mysql动态网站开发

phpmyadmin docker

php session id

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站