EventBufferEvent
PHP Manual

EventBufferEvent::connect

(PECL event >= 1.2.6-beta)

EventBufferEvent::connectConnect buffer event's socket to given address

Description

public bool EventBufferEvent::connect ( string $addr [, bool $sync_resolve ] )

Connect buffer event's socket to given address(optionally with port).

This function doesn't require sockets support. If socket is not assigned to the buffer event, this function allocates a socket and makes it non-blocking internally.

If sync_resolve parameter is TRUE, the function tries to resolve the hostname within addr syncronously (!). Otherwise addr parameter expected to be an IP address with optional port number.

To resolve DNS names asyncronously, use EventBufferEvent::connectHost() method.

Parameters

addr

If sync_resolve is TRUE, addr expected to be a hostname. Otherwise addr should contain an IP address. Recognized formats are:

[IPv6Address]:port
[IPv6Address]
IPv6Address
IPv4Address:port
IPv4Address

sync_resolve

Whether to syncronously resolve hostname specified by addr , or use addr as an IP address with optional port number.

Return Values

Returns TRUE on success. Otherwise FALSE.

Examples

Example #1 EventBufferEvent::connect() example

<?php
/*
 * 1. Connect to 127.0.0.1 at port 80
 * by means of EventBufferEvent::connect().
 *
 * 2. Request /index.cphp via HTTP/1.0
 * using the output buffer.
 *
 * 3. Asyncronously read the response and print it to stdout.
 */

/* Read callback */
function readcb($bev$base) {
    
$input $bev->getInput();

    while ((
$n $input->remove($buf1024)) > 0) {
        echo 
$buf;
    }
}

/* Event callback */
function eventcb($bev$events$base) {
    if (
$events EventBufferEvent::CONNECTED) {
        echo 
"Connected.\n";
    } elseif (
$events & (EventBufferEvent::ERROR EventBufferEvent::EOF)) {
        if (
$events EventBufferEvent::ERROR) {
            echo 
"DNS error: "$bev->getDnsErrorString(), PHP_EOL;
        }

        echo 
"Closing\n";
        
$base->exit();
        exit(
"Done\n");
    }
}

$base = new EventBase();

echo 
"step 1\n";
$bev = new EventBufferEvent($base/* use internal socket */ NULL,
    
EventBufferEvent::OPT_CLOSE_ON_FREE EventBufferEvent::OPT_DEFER_CALLBACKS);
if (!
$bev) {
    exit(
"Failed creating bufferevent socket\n");
}

echo 
"step 2\n";
$bev->setCallbacks("readcb"/* writecb */ NULL"eventcb"$base);
$bev->enable(Event::READ Event::WRITE);

echo 
"step 3\n";
/* Send request */
$output $bev->getOutput();
if (!
$output->add(
    
"GET /index.cphp HTTP/1.0\r\n".
    
"Connection: Close\r\n\r\n"
)) {
    exit(
"Failed adding request to output buffer\n");
}

/* Connect to the host syncronously.
 * We know the IP, and don't need to resolve DNS. */
if (!$bev->connect("127.0.0.1:80")) {
    exit(
"Can't connect to host\n");
}

/* Dispatch pending events */
$base->dispatch();

The above example will output something similar to:

step 1
step 2
step 3
Connected.
HTTP/1.1 200 OK
Server: nginx/1.2.6
Date: Sat, 09 Mar 2013 10:06:58 GMT
Content-Type: text/html; charset=utf-8
Connection: close
X-Powered-By: PHP/5.4.11--pl2-gentoo

sdfsdfsf
Closing
Done

See Also


EventBufferEvent
PHP Manual