• php-socket通信演示


    client:

    error_reporting(E_ALL);
    set_time_limit(0);
    echo "<h2>TCP/IP Connection</h2>
    ";
    $port = 1935;
    $ip = "127.0.0.1";
    /*
     +-------------------------------
     *    @socket连接整个过程
     +-------------------------------
     *    @socket_create
     *    @socket_connect
     *    @socket_write
     *    @socket_read
     *    @socket_close
     +--------------------------------
    */
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    // 第一个参数”AF_INET”用来指定域名;
    // 第二个参数”SOCK_STREM”告诉函数将创建一个什么类型的Socket(在这个例子中是TCP类型),UDP是SOCK_DGRAM
    
    if ($socket < 0) {
        echo "socket_create() failed: reason: " . socket_strerror($socket) . "
    ";
    } else {
        echo "OK.
    ";
    }
    echo "试图连接 '$ip' 端口 '$port'...
    ";
    $result = socket_connect($socket, $ip, $port);
    if ($result < 0) {
        echo "socket_connect() failed.
    Reason: ($result) " . socket_strerror($result) . "
    ";
    } else {
        echo "连接OK
    ";
    }
    $in = "Ho
    ";
    $in.= "first blood335555553
    ";
    $out = '';
    if (!socket_write($socket, $in, strlen($in))) {
        echo "socket_write() failed: reason: " . socket_strerror($socket) . "
    ";
    } else {
        echo "发送到服务器信息成功!
    ";
        echo "发送的内容为:<font color='red'>$in</font> <br>";
    }
    while ($out = socket_read($socket, 8192)) {
        echo "接收服务器回传信息成功!
    ";
        echo "接受的内容为:", $out;
    }
    echo "关闭SOCKET...
    ";
    socket_close($socket);
    echo "关闭OK
    ";
    View Code

    server:

    //确保在连接客户端时不会超时
    set_time_limit(0);
    $ip = '127.0.0.1';
    $port = 1935;
    /*
     +-------------------------------
     *    @socket通信整个过程
     +-------------------------------
     *    @socket_create
     *    @socket_bind
     *    @socket_listen
     *    @socket_accept
     *    @socket_read
     *    @socket_write
     *    @socket_close
     +--------------------------------
    */
    /*----------------    以下操作都是手册上的    -------------------*/
    
    if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) { // 创建一个Socket链接
        echo "socket_create() 失败的原因是:" . socket_strerror($sock) . "
    ";
    }
    
    if (($ret = socket_bind($sock, $ip, $port)) < 0) { //绑定Socket到端口
        echo "socket_bind() 失败的原因是:" . socket_strerror($ret) . "
    ";
    }
    if (($ret = socket_listen($sock, 4)) < 0) { // 开始监听链接链接
        echo "socket_listen() 失败的原因是:" . socket_strerror($ret) . "
    ";
    }
    $count = 0;
    do {
        if (($msgsock = socket_accept($sock)) < 0) { //堵塞等待另一个Socket来处理通信
            echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "
    ";
            break;
        } else {
            //发送消息到客户端
            $msg = "测试成功!
    ";
            socket_write($msgsock, $msg, strlen($msg)); 
            
            
            //接收客户端消息
            echo "测试成功了啊
    ";
            $buf = socket_read($msgsock, 8192); // 获得客户端的输入
            $talkback = "收到的信息:$buf
    ";
            echo $talkback;
            if (++$count >= 5) {
                break;
            };
        }
        //echo $buf;
        socket_close($msgsock);
    } while (true);
    socket_close($sock);
    View Code

    参考博主:http://www.cnblogs.com/nixi8/p/4904496.html

  • 相关阅读:
    用fnmatch函数进行字符通配
    activity和service之间的相互通信方法
    IGMP协议简介
    Android2.2快速入门
    Android开发之旅:HelloWorld项目的目录结构
    Android的五大基本组件
    Android Service 组件
    TCP交互数据流 成块数据流
    为什么要进行IP选路?
    embOS实时操作系统 任务通讯
  • 原文地址:https://www.cnblogs.com/isungge/p/7724404.html
Copyright © 2020-2023  润新知