Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: socket_connect - Manual
[go: Go Back, main page]

PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<socket_closesocket_create_listen" width="11" height="7"/>
view the version of this page
Last updated: Sat, 06 May 2006

socket_connect

(PHP 4 >= 4.1.0, PHP 5)

socket_connect -- ソケット上の接続を初期化する

説明

bool socket_connect ( resource socket, string address [, int port] )

ソケット記述子 socket を用いて接続を初期化 します。この記述子は、socket_create() で作成した 有効なソケット記述子である必要があります。

パラメータaddress には、 ソケットの種類が AF_INET の場合はドット区切り表記の IP アドレス(例: 127.0.0.1)、 AF_UNIX の場合は Unix ドメインソケットのパス名を 指定します。

パラメータ portAF_INET ソケットに接続する場合にのみ使用され、 接続するリモートホストのポートを指定します。

成功した場合に TRUE を、失敗した場合に FALSE を返します。 エラーコードは、 socket_last_error() により取得できます。 このコードを socket_strerror() に渡すことにより、 エラー内容を表すテキストを得ることができます。

socket_bind()socket_listen()socket_create()socket_last_error() および socket_strerror() も参照ください。



add a note add a note User Contributed Notes
socket_connect
ScriptBlue at nyc dot rr dot com
18-Sep-2005 01:13
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.
11-Jul-2005 11:26
well this solution (above) is not right because you cant ping port...
telefoontoestel at home
23-Nov-2003 05:05
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 ;-)
seymour@itsyourdomain
02-Oct-2003 04:43
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 at freemail dot ru
27-Aug-2003 05:54
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!
w at ff dot st
28-Jun-2003 06:20
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).
greg at mtechsolutions dot ca
01-May-2003 06:12
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).
logan at voerthegame dot com
25-Jul-2002 03:24
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";
           }

<socket_closesocket_create_listen" width="11" height="7"/>
 Last updated: Sat, 06 May 2006
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2006 The PHP Group
All rights reserved.
This mirror generously provided by: PacketBusiness, Inc.
Last updated: Mon Sep 4 04:23:30 2006 JST