1

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);
}
Quan
  • 39
  • 1
  • 6
  • And you only receive the warning intermittently? Is your client machine a VPS, shared hosting or a dedicated box? Also, are you sure the `$ip` address you're attempting to connect to is valid and not the reason for the error? –  Dec 18 '12 at 17:23
  • Yes, just that warning, and it goes to using fsockopen. I'm using a VPS and not sure if it happend the same on dedicated. Oh, as I said, the IP is correct and it runs fine. But sometimes PHP says "Invalid IP" and continue using fsockopen. Thank you. – Quan Dec 18 '12 at 18:03
  • This is just a *guess*, but I suspect the problem is that `fsockopen` periodically gets confused by your VPS's multiple IP addressses because you can't use a stream context to bind the socket to a specific IP address. Try eliminating the `else {` conditional (only use the `stream_socket_client` method) and my guess is that the problem will disappear. `fsockopen` is easier to use, but it doesn't provide the more advanced context options like `stream_socket_client` ... –  Dec 18 '12 at 18:13
  • Thank @rdlowrey. When stream_socket_client is applied the IP in the header of emails sent out are correct. But when fsocketopen is used, the IP in emails went to my VPS main IP. All scripts that using stream_socket_client are run under shell mode (command mode), do you think that causes the warning message? Thank you!!! – Quan Dec 19 '12 at 03:42

1 Answers1

0

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

Matt Williamson
  • 39,165
  • 10
  • 64
  • 72
  • 1.1.1.1 will be my server's public IP address. And :0 will connect to all ports. Am I doing right? – Quan Dec 18 '12 at 17:04
  • I don't believe the OP is trying to connect to the local machine. It's my understanding that a socket client is being created to connect to outside machines and that the OP wants to bind the client's origin IP address as one specific IP of the multiple IPs it has at its disposal. –  Dec 18 '12 at 17:04
  • 1
    You cannot connect to all ports. You need to connect to a specific port. – Matt Williamson Dec 18 '12 at 17:05
  • 1
    Oh, my mistake. :0 will let PHP chose what port to connect as it says in here: http://php.net/manual/en/context.socket.php. Is that correct? Thank you. – Quan Dec 18 '12 at 17:08
  • @rdlowrey you know what I mean :) – Quan Dec 18 '12 at 17:12
  • @MattWilliamson I'm using PHPMailer with some trick to make it use multiple IP when connect to mail server. Here is the code I fixed in class.smtp.php: `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); }` – Quan Dec 18 '12 at 17:23
  • Try echoing $host to make sure it's a valid address – Matt Williamson Dec 18 '12 at 17:33
  • @MattWilliamson yes, it is a valid address. Because I sent 100 emails with no problem. The warning "Invalid IP" sometimes happens and I don't know why? – Quan Dec 18 '12 at 17:43