范例
例子 1. Socket example: Simple TCP/IP serverThis example shows a simple talkback server. Change the address and port variables to suit your setup and execute. You may then connect to the server with a command similar to: telnet 192.168.1.53 10000 (where the address and port match your setup). Anything you type will then be output on the server side, and echoed back to you. To disconnect, enter 'quit'. #!/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 = '192.168.1.53';
$port = 10000;if (($sock = socket_create (AF_INET, SOCK_STREAM, 0)) < 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";
}do {
    if (($msgsock = socket_accept($sock)) < 0) {
        echo "socket_accept() failed: reason: " . socket_strerror ($msgsock) . "\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))) {
            echo "socket_read() failed: reason: " . socket_strerror ($ret) . "\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);
?>
 
例子 2. Socket example: Simple TCP/IP clientThis example shows a simple, one-shot HTTP client. It simply connects to a page, submits a HEAD request, echoes the reply, and exits. <?php
error_reporting (E_ALL);echo "<h2>TCP/IP Connection</h2>\n";/* Get the port for the WWW service. */
$service_port = getservbyname ('www', 'tcp');/* Get the IP address for the target host. */
$address = gethostbyname ('www.example.com');/* Create a TCP/IP socket. */
$socket = socket_create (AF_INET, SOCK_STREAM, 0);
if ($socket < 0) {
    echo "socket_create() failed: reason: " . socket_strerror ($socket) . "\n";
} else {
    echo "OK.\n";
}echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect ($socket, $address, $service_port);
if ($result < 0) {
    echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "\n";
} else {
    echo "OK.\n";
}$in = "HEAD / HTTP/1.0\r\n\r\n";
$out = '';echo "Sending HTTP HEAD request...";
socket_write ($socket, $in, strlen ($in));
echo "OK.\n";echo "Reading response:\n\n";
while ($out = socket_read ($socket, 2048)) {
    echo $out;
}echo "Closing socket...";
socket_close ($socket);
echo "OK.\n\n";
?>
 

解决方案 »

  1.   

    这个例子我试过,总是报下面的错误
    Warning: socket_bind() unable to bind address [0]: 在其上下文中,该请求的地址无效。 in H:\aircdphp\socket1.php on line 18Warning: socket_listen() unable to listen on socket [0]: 提供了一个无效的参数。 in 
    H:\aircdphp\socket1.php on line 22我用socket_connect和socket_send是可以的,不过在socket_read的时候就没有响应了不知道为什么
      

  2.   

    会不会是用户权限问题,默认的Local System用户没有使用网络的权限!
      

  3.   

    是管理员,没有权限的话socket_send也应该失败吧
      

  4.   

    发送你可以用GET或者POST,下例是手册上的;<?php
    $fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
    if (!$fp) {
        echo "$errstr ($errno)<br />\n";
    } else {
        $out = "GET / HTTP/1.1\r\n";
        $out .= "Host: www.example.com\r\n";
        $out .= "Connection: Close\r\n\r\n";    fwrite($fp, $out);
        while (!feof($fp)) {
            echo fgets($fp, 128);
        }
        fclose($fp);
    }
    ?>