-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
265 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
/** | ||
* require ./configure --enable-sockets | ||
*/ | ||
|
||
$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"); | ||
|
||
function socket_onRead($socket) | ||
{ | ||
static $i = 0; | ||
|
||
echo socket_read($socket, 8192)."\n"; | ||
$i ++; | ||
if ($i > 10) | ||
{ | ||
echo "finish\n"; | ||
swoole_event_del($socket); | ||
socket_close($socket); | ||
} | ||
else | ||
{ | ||
sleep(1); | ||
swoole_event_set($socket, null, 'socket_onWrite', SWOOLE_EVENT_READ | SWOOLE_EVENT_WRITE); | ||
} | ||
} | ||
|
||
function socket_onWrite($socket) | ||
{ | ||
socket_write($socket, "hi swoole"); | ||
swoole_event_set($socket, null, null, SWOOLE_EVENT_READ); | ||
} | ||
|
||
function socket_onConnect($socket) | ||
{ | ||
$err = socket_get_option($socket, SOL_SOCKET, SO_ERROR); | ||
if ($err == 0) | ||
{ | ||
echo "connect server success\n"; | ||
swoole_event_set($socket, null, null, SWOOLE_EVENT_READ); | ||
swoole_event_set($socket, null, 'socket_onWrite', SWOOLE_EVENT_READ); | ||
socket_write($socket, "first package\n"); | ||
} | ||
else | ||
{ | ||
echo "connect server failed\n"; | ||
swoole_event_del($socket); | ||
socket_close($socket); | ||
} | ||
} | ||
|
||
swoole_event_add($socket, 'socket_onRead', 'socket_onConnect', SWOOLE_EVENT_WRITE); | ||
@socket_connect($socket, '127.0.0.1', 9501); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
$fp = stream_socket_client("tcp://127.0.0.1:9501", $errno, $errstr, 30); | ||
if (!$fp) { | ||
exit("$errstr ($errno)<br />\n"); | ||
} | ||
fwrite($fp, "HELLO world"); | ||
|
||
function stream_onRead($fp) | ||
{ | ||
echo fread($fp, 1024)."\n"; | ||
sleep(1); | ||
swoole_event_set($fp, null, null, SWOOLE_EVENT_READ | SWOOLE_EVENT_WRITE); | ||
//swoole_event_del($fp); | ||
//fclose($fp); | ||
} | ||
|
||
function stream_onWrite($fp) | ||
{ | ||
fwrite($fp, "hi swoole\n"); | ||
swoole_event_set($fp, null, null, SWOOLE_EVENT_READ); | ||
} | ||
|
||
swoole_event_add($fp, 'stream_onRead', 'stream_onWrite'); | ||
|
||
echo "start\n"; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.