越简单越好!

PHP测试socket连接

发表于 2018-01-02 17:15 | 207次阅读 0次点赞   PHP

1. 创建PHP文件, test.php


<?php
function printLog($log)
{
    echo '[' . date('Y-m-d H:i:s', time()) . '] - ' . $log . "\n";
}

$tcpClient = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($tcpClient, SOL_SOCKET, SO_RCVTIMEO, ["sec"=>1, "usec"=>0]);
socket_set_option($tcpClient, SOL_SOCKET, SO_SNDTIMEO, ["sec"=>1, "usec"=>0]);
$connect = socket_connect($tcpClient, $argv[1], $argv[2]);
if(!$connect) {
    printLog('connect error');
    exit;
}
while(1) {
    $message = time();
    $write = socket_write($tcpClient, $message);
    printLog('Send Message:' . $message);
    usleep(1000000);
}
socket_shutdown($tcpClient);
socket_close($tcpClient);
?>
2. 执行测试


php test.php 192.168.1.100 1234

返回顶部 ^