dpwdldgn43486 2010-06-03 20:17
浏览 57
已采纳

适用于PHP的L-Soft LISTSERV TCPGUI接口

I'm trying to use LISTSERV's "API" in PHP. L-Soft calls this TCPGUI, and essentially, you can request data like over Telnet. To do this, I'm using PHP's TCP socket functions.

Here's the C code provided by L-Soft:

C: http://www.lsoft.com/manuals/16.0/htmlhelp/advanced%20topics/TCPGUI.html#2334328

I've provided the PHP conversion I'm using in my first answer. I hope this helps someone someday.

  • 写回答

1条回答 默认 最新

  • dqwh1208 2010-06-12 17:07
    关注

    This is a one command at a time function a colleague wrote. Still wondering if more can be done in terms of flexibility, efficiency, and speed.

    /*
     * lcmd - execute LISTSERV command
     *
     * This function connects to a LISTSERV host and executes a single command
     * using the supplied credentials.
     *
     * Returns result from command if successful; otherwise, returns FALSE.
     */
    function lcmd($host, $port, $user, $password, $cmd) {
        $request_header = "";
        $request_body = "";
        $response_header = "";
        $response_body = "";
        $length = 0;
        $status;
        $result;
    
        /*
     * Get host address
     */
    $host = gethostbyname($host);
    
    /*
     * Connect to server
     */
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    
    if (!$socket) {
        return FALSE;
    }
    
    $status = socket_connect($socket, $host, $port);
    
    if (!$status) {
        return FALSE;
    }
    
    /*
     * Format request body
     */
    $request_body = "${cmd} PW=${password}";
    
    $length = strlen($user) + strlen($request_body) + 1;
    
    /*
     * Format request header
     */
    $request_header = array();
    
    $request_header[] = "1B
    ";
    $request_header[] = chr((int)($length / 256));
    $request_header[] = chr($length % 255);
    $request_header[] = chr(strlen($user));
    $request_header[] = $user;
    
    $request_header = implode($request_header);
    
    /*
     * Send request header
     */
    $status = socket_send(
        $socket,
        $request_header,
        strlen($request_header),
        0
    );
    
    if (strlen($request_header) != $status) {
        return FALSE;
    }
    
    /*
     * Receive response header
     */
    while (socket_recv($socket, $b, 1, 0) == 1) {
        $response_header .= $b;
        if ("
    " == $b) {
            break;
        }
    }
    
    $status = (int)$response_header;
    
    /*
     * If status is not 250, then the is refusing the request.
     */
    if (250 != $status) {
        return FALSE;
    }
    
    /*
     * Send request body
     */
    $status = socket_send(
        $socket,
        $request_body,
        strlen($request_body),
        0
    );
    
    if (strlen($request_body) != $status) {
        return FALSE;
    }
    
    /*
     * Receive response body
     *
     * The first 8 bytes of the body is two unsigned 32-bit integers
     * that define the status of the command and the length of the
     * result.
     */
    $status = socket_recv($socket, $response_body, 8, 0);
    
    if (8 != $status) {
        return FALSE;
    }
    
    /*
     * Decode unsigned 32-bit big-endian integer status and result
     * length.
     */
    $response_body = str_split($response_body);
    
    $status = (
        (ord($response_body[0]) << 24 ) |
        (ord($response_body[1]) << 16 ) |
        (ord($response_body[2]) <<  8 ) |
        (ord($response_body[3]) <<  0 )
    );
    
    $length = (
        (ord($response_body[4]) << 24 ) |
        (ord($response_body[5]) << 16 ) |
        (ord($response_body[6]) <<  8 ) |
        (ord($response_body[7]) <<  0 )
    );
    
    /*
     * If status is not 0, then command execution has failed.
     */
    if (0 != $status) {
        return FALSE;
    }
    
    /*
     * Receive result
     */
    $status = socket_recv($socket, $result, $length, MSG_WAITALL);
    
    if ($length != $status) {
        return FALSE;
    }
    
    /*
     * Disconnect from server
     */
    socket_close($socket);
    
    /*
     * Return
     */
    return $result;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)