PHP服务器和ActionScript

时间:2016-05-31 11:38:02

标签: php sockets actionscript-3

我正在研究套接字通信,并成功连接PHP服务器和PHP客户端。现在我想用PHP服务器和ActionScript客户端做同样的事情。

http://php.net/manual/en/sockets.examples.php 我读了这篇文章,并且可以通过PHP中的2个Windows命令提示在服务器和客户端之间进行通信。 所以现在我将客户端更改为ActionScript,但它似乎连接到服务器,但不发送字符串或数字。

套接字通信是否只涉及地址和端口而不管语言?或者我不能用两种不同的语言进行交流?

对于服务器,我在上面的URL中使用了示例1代码,更改了地址和端口。 AS客户端在这里,我写了最小代码,这只是在文件运行时发送数字。:

import flash.net.Socket;
import flash.events.*;
import flash.net.LocalConnection;

var s:Socket = new Socket;
s.connect("localhost",10000);
s.writeByte( 10 );
s.flush();
trace("sent");

这是PHP服务器代码。

#!/usr/local/bin/php -q
<?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();

$address = 'localhost';
$port = 10000;

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}

if (socket_bind($sock, $address, $port) === false) {
    echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

if (socket_listen($sock, 5) === false) {
    echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

do {
    if (($msgsock = socket_accept($sock)) === false) {
        echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
        break;
    }
    /* Send instructions. */
    $msg = "\nWelcome to the PHP Test Server. \n" .
        "To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
    socket_write($msgsock, $msg, strlen($msg));

    do {
        if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
            echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
            break 2;
        }
        if (!$buf = trim($buf)) {
            continue;
        }
        if ($buf == 'quit') {
            break;
        }
        if ($buf == 'shutdown') {
            socket_close($msgsock);
            break 2;
        }
        $talkback = "PHP: You said '$buf'.\n";
        socket_write($msgsock, $talkback, strlen($talkback));
        echo "$buf\n";
    } while (true);
    socket_close($msgsock);
} while (true);

socket_close($sock);
?>

0 个答案:

没有答案