百科狗-知识改变命运!
--

socket_connect() - socket通信函数

乐乐12个月前 (11-21)阅读数 22#技术干货
文章标签参数

socket_connect()

(PHP 4 >= 4.1.0, PHP 5, PHP 7)

开启一个套接字连接

说明

socket_connect(resource $socket,string $address[,int $port= 0]): bool

socket_connect() - socket通信函数

用socket_create()创建的有效的套接字资源来连接到$address。

参数

$socket$address

如果参数$socket是AF_INET,那么参数$address则可以是一个点分四组表示法(例如127.0.0.1)的 IPv4 地址;如果支持 IPv6 并且$socket是AF_INET6,那么$address也可以是有效的 IPv6 地址(例如::1);如果套接字类型为AF_UNIX,那么$address也可以是一个Unix 套接字。

$port

参数$port仅仅用于AF_INETAF_INET6套接字连接的时候,并且是在此情况下是需要强制说明连接对应的远程服务器上的端口号。

返回值

成功时返回TRUE,或者在失败时返回FALSE。错误代码会传入socket_last_error(),如果将此参数传入socket_strerror()则可以得到错误的文字说明。

Note:

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

参见

  • socket_bind()给套接字绑定名字
  • socket_listen()Listens for a connection on a socket
  • socket_create()创建一个套接字(通讯节点)
  • socket_last_error()Returns the last error on the socket
  • socket_strerror()Return a string describing a socket error
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).
This will print the banner from a true 'telnet' server (router, switch, host, etc).
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, '127.0.0.1', 23);
while (TRUE) {
    $r = array($socket);
    $c = socket_select($r, $w = NULL, $e = NULL, 5);
    foreach ($r as $read_socket) {
        if ($r = negotiate($read_socket)) {
            var_dump($r);
            exit;
        }
    }
}
function negotiate ($socket) {
    socket_recv($socket, $buffer, 1024, 0);
    for ($chr = 0; $chr 
Hi there!
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 -v
In case it is, you can just type the command:
# setsebool httpd_can_network_connect=1
That's it... I read you had to reboot, but I didn't and it worked fine anyway. More info, you may check:
http://arkiv.netbsd.se/?ml=squirrelmail-users&a=2005-11&t=1523021
Here is an example of a non-blocking connect which should perform quite a bit faster than the one posted by Seymour below: 
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).
It seems that timeout values can be specified by setting the SO_SNDTIMEO option before calling socket_connect(): 
This will give you a simple port-checker.
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.

Greetz,
Peter.
rbarnes' tip is helpful, but I found that I needed to add a check for SOCKET_EISCONN in the while loop:
  ...
  $error = socket_last_error();
  if ($error == SOCKET_EISCONN) {
    $connected = true;
    break;
  }
  ...
At least on Mac OS X 10.5.
Just a heads up guys: make sure you're passing a properly formatted IP to your ping and socket functions.
E.g.: 192.168.0.18 -> OK
   192.168.0.018 -> Will result in "Unkwown host"
I was getting a 11004 error and could not solve it until I realized that was the problem.
(Pode ser que seja útil para alguém: verifique que o IP passado por parâmetro para o seu ping e funções socket é um endereço de IP corretamente formatado)
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. 
UDP sockets can be "connected", the subsystem will simply remember the destination. socket_getpeername() will also work. No data is sent upon UDP "connection". 
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.
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..... 
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.
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 ;-)
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)