Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Socket Functions : socket_connect

socket_connect

Initiates a connection on a socket (PHP 4 >= 4.0.7, PHP 5)
bool socket_connect ( resource socket, string address [, int port] )

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.

Examples ( Source code ) » socket_connect

<?php
$sock 
socket_create(AF_INETSOCK_STREAMSOL_TCP);
socket_set_nonblock($sock);
socket_connect($sock,"127.0.0.1"80);
socket_set_block($sock);
switch(
socket_select($r = array($sock), $w = array($sock), $f = array($sock), 5))
{
       case 
2:
               echo 
"[-] Connection Refused\n";
               break;
       case 
1:
               echo 
"[+] Connected\n";
               break;
       case 
0:
               echo 
"[-] Timeout\n";
               break;
}
?>

Code Examples / Notes » socket_connect

11-jul-2005 03:26

well this solution (above) is not right because you cant ping port...

w

man page for connect :
EINPROGRESS
The 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).


telefoontoestel

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 ;-)

greg

If you're using non-blocking, be sure not to turn it on until after you connect, otherwise you will get the mesasge:
PHP Warning:  socket_connect() unable to connect [115]: Operation now in progress in file.php on line 123
and socket_connect() will return false (even though it will connect).


scriptblue

If you do want to have a socket_connect operation timeout you can use the following code.
<?php
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_nonblock($sock);
socket_connect($sock,"127.0.0.1", 80);
socket_set_block($sock);
switch(socket_select($r = array($sock), $w = array($sock), $f = array($sock), 5))
{
       case 2:
               echo "[-] Connection Refused\n";
               break;
       case 1:
               echo "[+] Connected\n";
               break;
       case 0:
               echo "[-] Timeout\n";
               break;
}
?>
This basically makes socket_connect return immediately then you can use socket_select to see how the socket reacted.


logan

I had the same problem with the timeout, and i applied this solution.
It works only on linux PHP, i make a ping to the ip before connect the socket.....
$address = gethostbyname ($ip);
$command = "ping -c 1 " . $address;  
$r = exec($command);  
  if ($r[0]=="r")
  {
$socket = socket_create (AF_INET, SOCK_STREAM, 0);
if ($socket < 0) {
   echo "socket_create() failed: reason: " . socket_strerror ($socket) . "\n";
} else {
   echo "OK.\n";
}


seymour@itsyourdomain

here's how you can implement timeouts with the socket functions.
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.
<?
   $host = "127.0.0.1";
   $port = "80";
   $timeout = 15;  //timeout in seconds
   $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)
     or die("Unable to create socket\n");
   socket_set_nonblock($socket)
     or die("Unable to set nonblock on socket\n");
   $time = time();
   while (!@socket_connect($socket, $host, $port))
   {
     $err = socket_last_error($socket);
     if ($err == 115 || $err == 114)
     {
       if ((time() - $time) >= $timeout)
       {
         socket_close($socket);
         die("Connection timed out.\n");
       }
       sleep(1);
       continue;
     }
     die(socket_strerror($err) . "\n");
   }
   socket_set_block($this->socket)
     or die("Unable to set block on socket\n");
?>


net_del

function socket_raw_connect ($server, $port, $timeout,$request)
{
if (!is_numeric($port) or !is_numeric($timeout)) {return false;}
$socket = fsockopen($server, $port, $errno, $errstr, $timeout);
fputs($socket, $request);
$ret = '';
while (!feof($socket))
{
 $ret .= fgets($socket, 4096);
}
return $ret;
fclose($socket);
}
this code for easy raw connect.
Comment by net_del[nkg] (www.nkg.ru)
I am from russia. PHP is good language!


Change Language


Follow Navioo On Twitter
socket_accept
socket_bind
socket_clear_error
socket_close
socket_connect
socket_create_listen
socket_create_pair
socket_create
socket_get_option
socket_getpeername
socket_getsockname
socket_last_error
socket_listen
socket_read
socket_recv
socket_recvfrom
socket_select
socket_send
socket_sendto
socket_set_block
socket_set_nonblock
socket_set_option
socket_shutdown
socket_strerror
socket_write
eXTReMe Tracker