socket_connect

Description

Initiate a connection to address using the socket resource
socket, which must be a valid socket
resource created with socket_create().

Parameters

socket

address

The address parameter is either an IPv4 address
in dotted-quad notation (e.g. 127.0.0.1) if
socket is AF_INET, a valid
IPv6 address (e.g. ::1) if IPv6 support is enabled and
socket is AF_INET6
or the pathname of a Unix domain socket, if the socket family is
AF_UNIX.

port

The port parameter is only used and is mandatory
when connecting to an AF_INET or an
AF_INET6 socket, and designates
the port on the remote host to which a connection should be made.

Return Values

Returns TRUE on success or FALSE on failure. The error code can be retrieved with
socket_last_error(). This code may be passed to
socket_strerror() to get a textual explanation of the
error.

Note:

If the socket is non-blocking then this function returns FALSE with an
error Operation now in progress.

man page for connect : EINPROGRESSThe socket is non-blocking and the connection cannot be completed immediately. It is possible to select(2) or poll(2) for completion by selecting the socket for writing. After select indicates writability, use getsockopt(2) to read the SO_ERROR option at level SOL_SOCKET to determine whether connect completed successfully (SO_ERROR is zero) or unsuccessfully (SO_ERROR is one of the usual error codes listed here, explaining the reason for the failure).

use socket_getoption($socket,SOL_SOCKET,SO_ERROR) . If you get value 115, it is connecting. If you get value different than 115 and 0, that means that an error has occured (see what error with socket_strerror()).

However, I don't know how does that works under Windows, maybe it wont work at all. It is supposed to work under Linux (man pages said that).

For the TCP connections: socket_create(AF_INET, SOCK_STREAM, SOL_TCP);In case you're having problems in socket_connect() with socket_strerror() = "Permission denied", you may be having a SELinux config issue.

Check if SELinux is enabled:# /usr/sbin/sestatus -vIn case it is, you can just type the command:# setsebool httpd_can_network_connect=1

Note that on production-machines, you might want to alter the error reporting-level, since unsuccessful connects will give you a "No connection could be made because the target machine actively refused it"-error in the log.

Under Windows, make sure you enable the php_sockets.dll extension in your php.ini.

<?php $address=$_SERVER['REMOTE_ADDR'];

if (isset($_REQUEST['port']) and (!strlen($_REQUEST['port'])==0))$port=$_REQUEST['port']; else unset($port);

this example works for blocking sockets but will work for both blocking and nonblocking with minor modifications. first call to connect in nonblocking mode returns 115 EINPROGRESS, additional calls return 114 EALREADY if the connection has not already failed or succeeded. once the connection succeeds, the socket resource will be returned.

In reply to the function socket_raw_connect posted by "net_del at freemail dot ru". In the function you give a return value and afterwords you try to close the connection. That won't ever work. I think you want to alter your code ;-)

Note that as of PHP5.3 it is not possible to send IPv6 multicast to link-local addresses, because socket_connect() is just a trimmed-down version of connect() and does not support passing sin6_scope_id - the scope ID is required when sending packets to ff02::1 (all-nodes), for example.At first I thought I needed to bind the socket to the device using SO_BINDTODEVICE option (undefined constant in PHP - use numeric value 25), but it makes no difference, only requires root privileges to produce no usable results.Also, if you think you are sending multicast packets to link local addresses just because socket_sendto() returns a positive number of bytes, you might be wrong - just returning success does not mean that packets are sent over any link at all. In my test case I was sending to ff02::1, I could detect no errors, but Wireshark showed no packets. They end up in void.This is irrelevant to the handling of local reception sockets, so UDP listeners should still work as usual with IPv6/UDP. You might want to resort to C for implementing multicasters, though.

This probably sounds like common sense, but it is something nobody i asked thought of... you can't bind the socket to localhost, you must bind it to either the IP your router assigns you, or your public IP address. If you bind to localhost, it will give an invalid resource error.