PHP下socket编程

下面是一些简单的例子,在命令行运行php脚本就行

[命令行运行PHP]PHP中有一个php.exe文件,可以用命令执行PHP脚本。如:D:/php.exe -f F:/test.php ; 可以使用php.exe -h查看更多参数 

服务器端:

<?php

/**

 * 服务器端代码

 *

 */

//确保在连接客户端时不会超时

set_time_limit(0);

//设置IP和端口号

$address = "localhost";

$port = 1234; //调试的时候,可以多换端口来测试程序!

//创建一个SOCKET

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false)

{

    echo "socket_create() 失败的原因是:" . socket_strerror(socket_last_error()) . "/n";

    die;

}

//阻塞模式

if (socket_set_block($sock) == false)

{

    echo "socket_set_block() 失败的原因是:" . socket_strerror(socket_last_error()) . "/n";

    die;

}

//绑定到socket端口

if (socket_bind($sock, $address, $port) == false)

{

    echo "socket_bind() 失败的原因是:" . socket_strerror(socket_last_error()) . "/n";

    die;

}

//开始监听

if (socket_listen($sock, 4) == false)

{

    echo "socket_listen() 失败的原因是:" . socket_strerror(socket_last_error()) . "/n";

    die;

}

do

{

    if (($msgsock = socket_accept($sock)) === false)

    {

        echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error()) . "/n";

        die;

    }

    //发到客户端

    $msg = "welcome /n";

    if (socket_write($msgsock, $msg, strlen($msg)) === false)

    {

        echo "socket_write() failed: reason: " . socket_strerror(socket_last_error()) ."/n";

        die;

    }

    echo "读取客户端发来的信息/n";

    $buf = socket_read($msgsock, 8192);

    echo "收到的信息: $buf   /n";

   

    socket_close($msgsock);

} while (true);

socket_close($sock);

?>

客服端

<?php

/**

 * 客户端代码

 */

 

error_reporting(0);

set_time_limit(0);

echo " TCP/IP Connection /n";

$service_port = 10001;

$address = '127.0.0.1';

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

if ($socket === false)

{

    die;

}

else

{

    echo "OK";

}

echo "试图连接 ";

if (socket_connect($socket, $address, $service_port) == false)

{

    $error = socket_strerror(socket_last_error());

    echo "socket_connect() failed./n","Reason: {$error} /n";

    die;

}

else

{

    echo "连接OK/n";

}

$in   = "Hello World/r/n";

if (socket_write($socket, $in, strlen($in)) === false)

{

    echo "socket_write() failed: reason: " . socket_strerror(socket_last_error()) ."/n";

    die;

}

else

{

    echo "发送到服务器信息成功!/n","发送的内容为: $in  /n";

}

$out  = "";

while ($out = socket_read($socket, 8192))

{

    echo "接受的内容为: ".$out;

}

echo "关闭SOCKET…/n";

socket_close($socket);

echo "关闭OK/n";

?>

 

 

 

下面是我自己的代码:这是一个PHP客户端测试程序,目的是与服务器连接,将一个包体中的内容发给服务器,服务器查询数据库,然后将结果返还给客户端

<?                                                                        
$clientfd = socket_create(AF_INET, SOCK_STREAM, 0);
if($clientfd == -1)
{
    printf("failed to create socket!\n");
}

$server_port = 63214;
$server_addr = "10.1.1.65";

if((socket_connect($clientfd, $server_addr, $server_port)) === false)
{
    echo "failed to connect!\n";
    die;
}
else
{
    echo "succeed to connect!\n";
}

$pkg_len = 18;
$cmd_id  = 1;
$user_id =100;


$buf = pack("LLSLL", $pkg_len, 0, $cmd_id, 0, $user_id);
if(socket_write($clientfd, $buf, strlen($buf)) === false)
{
    echo "failed to send!\n";
}
else
{
    echo "succeed to send message to the server!\n";
    if(($result = socket_read($clientfd, 63214)))
    {
        $res = unpack($result);
        echo "succeed to receive data from the server:$server_addr\n";--
        echo "the result is :$res[1]\n";
    }
    else
    {
        echo "failed to receive data from the server:$server_addr\n";

    }
}

 

你可能感兴趣的:(PHP下socket编程)