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

1- when we use stream_socket_client() for connect from client A to B. we specify port of B and connect to it and port of A is random. can we specify port of A too ??

2- I use this code for accepting incoming connectiongs:

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

now how detect ip and port of incoming connection ??

  1. Yes, you can.

    $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. You can get client socket information using stream_socket_get_name function. $a = stream_socket_server('tcp://localhost:9056'); $b = stream_socket_accept($a); $addr = stream_socket_get_name($b,true); $c = fread($b,1000);