socket基礎

<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 5                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at the following url:           |
// | http://www.php.net/license/3_0.txt.                                  |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Original Author <author@example.com>                        |
// |          Your Name <you@example.com>                                 |
// +----------------------------------------------------------------------+
//
// $Id:$

set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$connection = socket_connect($socket, 'localhost', 1337);
while ($buffer = socket_read($socket, 1024, PHP_NORMAL_READ)) {
    if ($buffer == 'NO DATA') {
        echo ('

<p>NO DATA</p>

');
        break;
    } else {
        echo ('

<p>Buffer Data:' . $buffer . '</p>

');
    }
}
echo ('

<p>Writing to Socket</p>

');
if (!socket_write($socket, 'SOME DATArn')) {
    echo ('

<p>Write failed</p>

');
}
while ($buffer = socket_read($socket, 1024, PHP_NORMAL_READ)) {
    echo ('

<p>Data sent was: SOME DATA<br> Response was:' . $buffer . '</p>

');
}
echo ('

<p>Done Reading from Socket</p>

');


/*
socket_accept()    接受一個Socket鏈接
socket_bind()     把socket綁定在一個IP地址和端口上
socket_clear_error()   清除socket的錯誤或者最後的錯誤代碼
socket_close()     關閉一個socket資源
socket_connect()    開始一個socket鏈接
socket_create_listen()   在指定端口打開一個socket監聽
socket_create_pair()   產生一對沒有區別的socket到一個數組裏
socket_create()    產生一個socket,至關於產生一個socket的數據結構
socket_get_option()    獲取socket選項
socket_getpeername()   獲取遠程相似主機的ip地址
socket_getsockname()   獲取本地socket的ip地址
socket_iovec_add()    添加一個新的向量到一個分散/聚合的數組
socket_iovec_alloc()   這個函數建立一個可以發送接收讀寫的iovec數據結構
socket_iovec_delete()   刪除一個已經分配的iovec
socket_iovec_fetch()   返回指定的iovec資源的數據
socket_iovec_free()    釋放一個iovec資源
socket_iovec_set()    設置iovec的數據新值
socket_last_error()    獲取當前socket的最後錯誤代碼
socket_listen()     監聽由指定socket的全部鏈接
socket_read()     讀取指定長度的數據
socket_readv()     讀取從分散/聚合數組過來的數據
socket_recv()     從socket裏結束數據到緩存
socket_recvfrom()    接受數據從指定的socket,若是沒有指定則默認當前socket
socket_recvmsg()    從iovec裏接受消息
socket_select()     多路選擇
socket_send()     這個函數發送數據到已鏈接的socket
socket_sendmsg()    發送消息到socket
socket_sendto()    發送消息到指定地址的socket
socket_set_block()    在socket裏設置爲塊模式
socket_set_nonblock()   socket裏設置爲非塊模式
socket_set_option()    設置socket選項
socket_shutdown()    這個函數容許你關閉讀、寫、或者指定的socket
socket_strerror()    返回指定錯誤號的詳細錯誤
socket_write()     寫數據到socket緩存
socket_writev()    寫數據到分散/聚合數組
*/
相關文章
相關標籤/搜索