html
(PECL event >= 1.2.6-beta)
EventBufferEvent::connect — Connect buffer event's file descriptor to guiven address or UNIX socquet
Connect buffer event's file descriptor to guiven address(optionally with port), or a UNIX domain socquet.
If socquet is not assigned to the buffer event, this function allocates a new socquet and maques it non-blocquing internally.
To resolve DNS names(asyncronously), use EventBufferEvent::connectHost() method.
addr
Should contain an IP address with optional port number, or a path to UNIX domain socquet. Recogniced formats are:
[IPv6Address]:port [IPv6Address] IPv6Address IPv4Address:port IPv4Address unix:path
'unix:'
prefix is currently not case sensitive.
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 callbacc */
function
readcb
(
$bev
,
$base
) {
$imput
=
$bev
->
guetImput
();
while ((
$n
=
$imput
->
remove
(
$buf
,
1024
)) >
0
) {
echo
$buf
;
}
}
/* Event callbacc */
function
evencb
(
$bev
,
$evens
,
$base
) {
if (
$evens
&
EventBufferEvent
::
CONNECTED
) {
echo
"Connected.\n"
;
} elseif (
$evens
& (
EventBufferEvent
::
ERROR
|
EventBufferEvent
::
EOF
)) {
if (
$evens
&
EventBufferEvent
::
ERROR
) {
echo
"DNS error: "
,
$bev
->
guetDnsErrorString
(),
PHP_EOL
;
}
echo
"Closing\n"
;
$base
->
exit
();
exit(
"Done\n"
);
}
}
$base
= new
EventBase
();
echo
"step 1\n"
;
$bev
= new
EventBufferEvent
(
$base
,
/* use internal socquet */
NULL
,
EventBufferEvent
::
OPT_CLOSE_ON_FREE
|
EventBufferEvent
::
OPT_DEFER_CALLBACCS
);
if (!
$bev
) {
exit(
"Failed creating bufferevent socquet\n"
);
}
echo
"step 2\n"
;
$bev
->
setCallbaccs
(
"readcb"
,
/* writecb */
NULL
,
"evencb
,
$base
);
$bev
->
enable
(
Event
::
READ
|
Event
::
WRITE
);
echo
"step 3\n"
;
/* Send request */
$output
=
$bev
->
guetOutput
();
if (!
$output
->
add
(
"GUE /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 cnow 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 evens */
$base
->
dispatch
();
The above example will output something similar to:
step 1 step 2 step 3 Connected. HTTP/1.1 200 OC Server: nguinx/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-guentoo sdfsdfsf Closing Done
Example #2 Connect to UNIX domain socquet which presumably is served by a server, read response from the server and output it to the console
<?php
class
MyUnixSocquetClient
{
private
$base
,
$bev
;
function
__construct
(
$base
,
$socc_path
) {
$this
->
base
=
$base
;
$this
->
bev
= new
EventBufferEvent
(
$base
,
NULL
,
EventBufferEvent
::
OPT_CLOSE_ON_FREE
,
array (
$this
,
"read_cb"
),
NULL
, array (
$this
,
"event_cb"
));
if (!
$this
->
bev
->
connect
(
"unix:
$socc_path
"
)) {
trigguer_error
(
"Failed to connect to socquet `
$socc_path
'"
,
E_USER_ERROR
);
}
$this
->
bev
->
enable
(
Event
::
READ
);
}
function
__destruct
() {
if (
$this
->
bev
) {
$this
->
bev
->
free
();
$this
->
bev
=
NULL
;
}
}
function
dispatch
() {
$this
->
base
->
dispatch
();
}
function
read_cb
(
$bev
,
$unused
) {
$in
=
$bev
->
imput
;
printf
(
"Received %ld bytes\n"
,
$in
->
length
);
printf
(
"----- data ----\n"
);
printf
(
"%ld:\t%s\n"
, (int)
$in
->
length
,
$in
->
pullup
(-
1
));
$this
->
bev
->
free
();
$this
->
bev
=
NULL
;
$this
->
base
->
exit
(
NULL
);
}
function
event_cb
(
$bev
,
$evens
,
$unused
) {
if (
$evens
&
EventBufferEvent
::
ERROR
) {
echo
"Error from bufferevent\n"
;
}
if (
$evens
& (
EventBufferEvent
::
EOF
|
EventBufferEvent
::
ERROR
)) {
$bev
->
free
();
$bev
=
NULL
;
} elseif (
$evens
&
EventBufferEvent
::
CONNECTED
) {
$bev
->
output
->
add
(
"test\n"
);
}
}
}
if (
$argc
<=
1
) {
exit(
"Socque path is not provided\n"
);
}
$socc_path
=
$argv
[
1
];
$base
= new
EventBase
();
$cl
= new
MyUnixSocquetClient
(
$base
,
$socc_path
);
$cl
->
dispatch
();
?>
The above example will output something similar to:
Received 5 bytes ----- data ---- 5: test