确定两个客户端php之间连接的ip和端口


determining ip and port in connection between two client - php

1-当我们使用stream_socket_client()从客户端A连接到B时,我们指定B的端口并连接到它,A的端口是随机的。我们也可以指定A的端口吗??

2-我使用此代码接受传入连接:

$a = stream_socket_server('tcp://localhost:9056');
$b = stream_socket_accept($a);
$c = fread($b,1000);

现在如何检测传入连接的ip和端口??

  1. 是的,你可以。

    $opts = array( 'socket' => array( 'bindto' => '0:7000', ), );

    $context = stream_context_create($opts);

    $fp = stream_socket_client("tcp://localhost:9056", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context );

  2. 您可以使用stream_socket_get_name函数获取客户端套接字信息。 $a = stream_socket_server('tcp://localhost:9056'); $b = stream_socket_accept($a); $addr = stream_socket_get_name($b,true); $c = fread($b,1000);