<?php
error_reporting(E_ALL);
/* Allow the script to hang around waiting for connections. */
set_time_limit (0);
/* Turn on implicit output flushing so we see what we're getting
* as it comes in. */
ob_implicit_flush ();
//服务器ip和端口
$address = '127.0.0.1';
$port = 11915;
//启动服务
if (($sock = socket_create (AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
echo "socket_create() failed: reason: " . socket_strerror ($sock) . "\n";
}
//绑定
if (($ret = socket_bind ($sock, $address, $port)) < 0) {
echo "socket_bind() failed: reason: " . socket_strerror ($ret) . "\n";
}
//侦听
if (($ret = socket_listen ($sock, 5)) < 0) {
echo "socket_listen() failed: reason: " . socket_strerror ($ret) . "\n";
}
echo "connect................";
//等待连接...
do {
//接受连接
if (($msgsock = socket_accept ($sock)) < 0) {
echo "socket_accept() failed: reason: " . socket_strerror ($msgsock) . "\n";
break;
}
$msg = "\nWelcome to the PHP Test Server. \n" .
"To quit, type 'quit'. To shut down the server type 'shutdown'.\n\0";
echo "some one connected";
//给客户端发欢迎消息
socket_send ($msgsock, $msg, strlen ($msg),0);
//等待客户端发来的消息
do {
//接受消息
socket_recv ($msgsock, $buf, 2048, 0);
if (false === ($buf = socket_read ($msgsock, 2048))) {
echo "socket_read() failed: reason: " . socket_strerror ($ret) . "\n";
break 2;
}
//flash端关闭的时候会给服务端发送一个空消息,判断退出
if ($buf == '') {
socket_close ($msgsock);
echo "some one quit";
break;
}
//把客户端发送的消息群发
$talkback = "You said: $buf";
echo $talkback;
socket_send ($msgsock, $talkback, strlen ($talkback),0);
} while (true);
//关闭客户端
socket_close ($msgsock);
} while (true);
//关闭服务器端
socket_close ($sock);
?>
注意,这些代码需要在cli模式下运行(直接使用网页运行会出错,而且会出现端口被专用),进入命令行模式,切换到php目录,输入下面命令:
程序代码
php.exe socket.php
flash端测试代码,完全是测试代码,也可以使用我以前公布的flash端代码,通用的:
程序代码
var socket = new XMLSocket();
socket.onConnect = function (success:Boolean) {
if (success) {
trace ("Connection succeeded!")
} else {
trace ("Connection failed!")
}
}
if (!socket.connect("127.0.0.1", 11915)) {
trace("connectionFailed");
} else {
trace("waitForConnection");
}
socket.onData = function (doc) {
trace("asddddddddddd=="+doc.length)
trace(doc);
}
_root.onEnterFrame = function()
{
if(Key.isDown(Key.END))
{
socket.send("roading..");
trace("send")
}
if(Key.isDown(40))
{
socket.close();
}
}
error_reporting(E_ALL);
/* Allow the script to hang around waiting for connections. */
set_time_limit (0);
/* Turn on implicit output flushing so we see what we're getting
* as it comes in. */
ob_implicit_flush ();
//服务器ip和端口
$address = '127.0.0.1';
$port = 11915;
//启动服务
if (($sock = socket_create (AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
echo "socket_create() failed: reason: " . socket_strerror ($sock) . "\n";
}
//绑定
if (($ret = socket_bind ($sock, $address, $port)) < 0) {
echo "socket_bind() failed: reason: " . socket_strerror ($ret) . "\n";
}
//侦听
if (($ret = socket_listen ($sock, 5)) < 0) {
echo "socket_listen() failed: reason: " . socket_strerror ($ret) . "\n";
}
echo "connect................";
//等待连接...
do {
//接受连接
if (($msgsock = socket_accept ($sock)) < 0) {
echo "socket_accept() failed: reason: " . socket_strerror ($msgsock) . "\n";
break;
}
$msg = "\nWelcome to the PHP Test Server. \n" .
"To quit, type 'quit'. To shut down the server type 'shutdown'.\n\0";
echo "some one connected";
//给客户端发欢迎消息
socket_send ($msgsock, $msg, strlen ($msg),0);
//等待客户端发来的消息
do {
//接受消息
socket_recv ($msgsock, $buf, 2048, 0);
if (false === ($buf = socket_read ($msgsock, 2048))) {
echo "socket_read() failed: reason: " . socket_strerror ($ret) . "\n";
break 2;
}
//flash端关闭的时候会给服务端发送一个空消息,判断退出
if ($buf == '') {
socket_close ($msgsock);
echo "some one quit";
break;
}
//把客户端发送的消息群发
$talkback = "You said: $buf";
echo $talkback;
socket_send ($msgsock, $talkback, strlen ($talkback),0);
} while (true);
//关闭客户端
socket_close ($msgsock);
} while (true);
//关闭服务器端
socket_close ($sock);
?>
注意,这些代码需要在cli模式下运行(直接使用网页运行会出错,而且会出现端口被专用),进入命令行模式,切换到php目录,输入下面命令:
程序代码
php.exe socket.php
flash端测试代码,完全是测试代码,也可以使用我以前公布的flash端代码,通用的:
程序代码
var socket = new XMLSocket();
socket.onConnect = function (success:Boolean) {
if (success) {
trace ("Connection succeeded!")
} else {
trace ("Connection failed!")
}
}
if (!socket.connect("127.0.0.1", 11915)) {
trace("connectionFailed");
} else {
trace("waitForConnection");
}
socket.onData = function (doc) {
trace("asddddddddddd=="+doc.length)
trace(doc);
}
_root.onEnterFrame = function()
{
if(Key.isDown(Key.END))
{
socket.send("roading..");
trace("send")
}
if(Key.isDown(40))
{
socket.close();
}
}