douzengjian1535 2012-12-18 16:58 采纳率: 100%
浏览 61

PHP流套接字客户端 - 无效的IP地址

My server has multiple IP address, and I use this code to bind IP to stream socket client. It works fine. But some time I get this warning:

stream_socket_client(): Invalid IP Address:

I'm using PHP 5.3.3 and my server is running CentOS 5.x

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


// create the context...
$context = stream_context_create($opts);
$fp = @stream_socket_client ( $link, $errno, $errstr, 120, STREAM_CLIENT_CONNECT, $context);

Can you tell me how to avoid this warning. Thank you very much!!!


UPDATE

Code as requested by a comment below:

if (function_exists('stream_context_create')
    && function_exists('stream_socket_client')
) {
    $socket_options = array('socket' => array('bindto' => $ip .':0'));
    $socket_context = stream_context_create($socket_options);
    $this->smtp_conn = stream_socket_client(
        "$host:$port",
        $errno,
        $errstr,
        30,
        STREAM_CLIENT_CONNECT,
        $socket_context
    );
} else {
    $this->smtp_conn = @fsockopen($host, $port, $errno, $errstr, $tval);
}
  • 写回答

1条回答 默认 最新

  • dpwh11290 2012-12-18 17:02
    关注

    That is an invalid IP address... you'll also need a correct port.

    If you are connecting to your local machine, the IP will be 127.0.0.1

    评论

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样