當前位置:首頁 » 編程語言 » phpsocket

phpsocket

發布時間: 2022-01-12 14:43:08

A. 什麼是socketphp如何實現socket通信

網路上的兩個程序通過一個雙向的通信連接實現數據的交換,這個連接的一端稱為一個socket。
建立網路通信連接至少要一對埠號(socket)。socket本質是編程介面(API),對TCP/IP的封裝,TCP/IP也要提供可供程序員做網路開發所用的介面,這就是Socket編程介面;HTTP是轎車,提供了封裝或者顯示數據的具體形式;Socket是發動機,提供了網路通信的能力。
PHP在socket開發上不是很給力,可以考慮使用workerman~

B. php socket可以做什麼

PHP 使用Berkley的socket庫來創建它的連接。你可以知道socket只不過是一個數據結構。你使用這個socket數據結構去開始一個客戶端和伺服器之間的會話。這個伺服器是一直在監聽准備產生一個新的會話。當一個客戶端連接伺服器,它就打開伺服器正在進行監聽的一個埠進行會話。這時,伺服器端接受客戶端的連接請求,那麼就進行一次循環。現在這個客戶端就能夠發送信息到伺服器,伺服器也能發送信息給客戶端。
產生一個Socket,你需要三個變數:一個協議、一個socket類型和一個公共協議類型。產生一個socket有三種協議供選擇,繼續看下面的內容來獲取詳細的協議內容。
定義一個公共的協議類型是進行連接一個必不可少的元素。下面的表我們看看有那些公共的協議類型。

表一:協議
名字/常量 描述
AF_INET 這是大多數用來產生socket的協議,使用TCP或UDP來傳輸,用在IPv4的地址
AF_INET6 與上面類似,不過是來用在IPv6的地址
AF_UNIX 本地協議,使用在Unix和linux系統上,它很少使用,一般都是當客戶端和伺服器在同一台及其上的時候使用
表二:Socket類型
名字/常量 描述
SOCK_STREAM 這個協議是按照順序的、可靠的、數據完整的基於位元組流的連接。這是一個使用最多的socket類型,這個socket是使用TCP來進行傳輸。
SOCK_DGRAM 這個協議是無連接的、固定長度的傳輸調用。該協議是不可靠的,使用UDP來進行它的連接。
SOCK_SEQPACKET 這個協議是雙線路的、可靠的連接,發送固定長度的數據包進行傳輸。必須把這個包完整的接受才能進行讀取。
SOCK_RAW 這個socket類型提供單一的網路訪問,這個socket類型使用ICMP公共協議。(ping、traceroute使用該協議)
SOCK_RDM 這個類型是很少使用的,在大部分的操作系統上沒有實現,它是提供給數據鏈路層使用,不保證數據包的順序

表三:公共協議
名字/常量 描述
ICMP 互聯網控制消息協議,主要使用在網關和主機上,用來檢查網路狀況和報告錯誤信息
UDP 用戶數據報文協議,它是一個無連接,不可靠的傳輸協議
TCP 傳輸控制協議,這是一個使用最多的可靠的公共協議,它能保證數據包能夠到達接受者那兒,如果在傳輸過程中發生錯誤,那麼它將重新發送出錯數據包。

現在你知道了產生一個socket的三個元素,那麼我們就在php中使用socket_create()函數來產生一個socket。這個 socket_create()函數需要三個參數:一個協議、一個socket類型、一個公共協議。socket_create()函數運行成功返回一個包含socket的資源類型,如果沒有成功則返回false。
Resourece socket_create(int protocol, int socketType, int commonProtocol);

現在你產生一個socket,然後呢?php提供了幾個操縱socket的函數。你能夠綁定socket到一個IP,監聽一個socket的通信,接受一個socket;現在我們來看一個例子,了解函數是如何產生、接受和監聽一個socket。

<?php
$commonProtocol = getprotobyname(「tcp」);
$socket = socket_create(AF_INET, SOCK_STREAM, $commonProtocol);
socket_bind($socket, 『localhost』, 1337);
socket_listen($socket);
// More socket functionality to come
?>

上面這個例子產生一個你自己的伺服器端。例子第一行,
$commonProtocol = getprotobyname(「tcp」);
使用公共協議名字來獲取一個協議類型。在這里使用的是TCP公共協議,如果你想使用UDP或者ICMP協議,那麼你應該把getprotobyname() 函數的參數改為「udp」或「icmp」。還有一個可選的辦法是不使用getprotobyname()函數而是指定SOL_TCP或SOL_UDP在 socket_create()函數中。
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
例子的第二行是產生一個socket並且返回一個socket資源的實例。在你有了一個socket資源的實例以後,你就必須把socket綁定到一個IP地址和某一個埠上。
socket_bind($socket, 『localhost』, 1337);
在這里你綁定socket到本地計算機(127.0.0.1)和綁定socket到你的1337埠。然後你就需要監聽所有進來的socket連接。
socket_listen($socket);
在第四行以後,你就需要了解所有的socket函數和他們的使用。

表四:Socket函數
函數名 描述
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() 寫數據到分散/聚合數組

(注: 函數介紹刪減了部分原文內容,函數詳細使用建議參考英文原文,或者參考PHP手冊)

以上所有的函數都是PHP中關於socket的,使用這些函數,你必須把你的socket打開,如果你沒有打開,請編輯你的php.ini文件,去掉下面這行前面的注釋:
extension=php_sockets.dll
如果你無法去掉注釋,那麼請使用下面的代碼來載入擴展庫:
<?php
if(!extension_loaded(『sockets』))
{
if(strtoupper(substr(PHP_OS, 3)) == 「WIN」)
{
dl(『php_sockets.dll』);
}
else
{
dl(『sockets.so』);
}
}
?>

如果你不知道你的socket是否打開,那麼你可以使用phpinfo()函數來確定socket是否打開。你通過查看phpinfo信息了解socket是否打開。如下圖:

查看phpinfo()關於socket的信息

◆產生一個伺服器

現在我們把第一個例子進行完善。你需要監聽一個指定的socket並且處理用戶的連接。

<?php
$commonProtocol = getprotobyname("tcp");
$socket = socket_create(AF_INET, SOCK_STREAM, $commonProtocol);
socket_bind($socket, 'localhost', 1337);
socket_listen($socket);
// Accept any incoming connections to the server
$connection = socket_accept($socket);
if($connection)
{
socket_write($connection, "You have connected to the socket.../n/r");
}
?>

你應該使用你的命令提示符來運行這個例子。理由是因為這里將產生一個伺服器,而不是一個Web頁面。如果你嘗試使用Web瀏覽器來運行這個腳本,那麼很有可能它會超過30秒的限時。你可以使用下面的代碼來設置一個無限的運行時間,但是還是建議使用命令提示符來運行。
set_time_limit(0);
在你的命令提示符中對這個腳本進行簡單測試:
Php.exe example01_server.php
如果你沒有在系統的環境變數中設置php解釋器的路徑,那麼你將需要給php.exe指定詳細的路徑。當你運行這個伺服器端的時候,你能夠通過遠程登陸(telnet)的方式連接到埠1337來測試這個伺服器。如下圖:

上面的伺服器端有三個問題:1. 它不能接受多個連接。2. 它只完成唯一的一個命令。3. 你不能通過Web瀏覽器連接這個伺服器。
這個第一個問題比較容易解決,你可以使用一個應用程序去每次都連接到伺服器。但是後面的問題是你需要使用一個Web頁面去連接這個伺服器,這個比較困難。你可以讓你的伺服器接受連接,然後些數據到客戶端(如果它一定要寫的話),關閉連接並且等待下一個連接。
在上一個代碼的基礎上再改進,產生下面的代碼來做你的新伺服器端:

<?php
// Set up our socket
$commonProtocol = getprotobyname("tcp");
$socket = socket_create(AF_INET, SOCK_STREAM, $commonProtocol);
socket_bind($socket, 'localhost', 1337);
socket_listen($socket);
// Initialize the buffer
$buffer = "NO DATA";
while(true)
{
// Accept any connections coming in on this socket

$connection = socket_accept($socket);
printf("Socket connected/r/n");
// Check to see if there is anything in the buffer
if($buffer != "")
{
printf("Something is in the buffer...sending data.../r/n");
socket_write($connection, $buffer . "/r/n");
printf("Wrote to socket/r/n");
}
else
{
printf("No Data in the buffer/r/n");
}
// Get the input
while($data = socket_read($connection, 1024, PHP_NORMAL_READ))
{
$buffer = $data;
socket_write($connection, "Information Received/r/n");
printf("Buffer: " . $buffer . "/r/n");
}
socket_close($connection);
printf("Closed the socket/r/n/r/n");
}
?>

這個伺服器端要做什麼呢?它初始化一個socket並且打開一個緩存收發數據。它等待連接,一旦產生一個連接,它將列印「Socket connected」在伺服器端的屏幕上。這個伺服器檢查緩沖區,如果緩沖區里有數據,它將把數據發送到連接過來的計算機。然後它發送這個數據的接受信息,一旦它接受了信息,就把信息保存到數據里,並且讓連接的計算機知道這些信息,最後關閉連接。當連接關閉後,伺服器又開始處理下一次連接。(翻譯的爛,附上原文)
This is what the server does. It initializes the socket and the buffer that you use to receive
and send data. Then it waits for a connection. Once a connection is created it prints
「Socket connected」 to the screen the server is running on. The server then checks to see if
there is anything in the buffer; if there is, it sends the data to the connected computer.
After it sends the data it waits to receive information. Once it receives information it stores
it in the data, lets the connected computer know that it has received the information, and
then closes the connection. After the connection is closed, the server starts the whole
process again.

◆產生一個客戶端

處理第二個問題是很容易的。你需要產生一個php頁連接一個socket,發送一些數據進它的緩存並處理它。然後你又個處理後的數據在還頓,你能夠發送你的數據到伺服器。在另外一台客戶端連接,它將處理那些數據。
To solve the second problem is very easy. You need to create a PHP page that connects to
a socket, receive any data that is in the buffer, and process it. After you have processed the
data in the buffer you can send your data to the server. When another client connects, it
will process the data you sent and the client will send more data back to the server.

下面的例子示範了使用socket:

<?php
// Create the socket and connect
$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
{
// Do something with the data in the buffer
echo(「<p>Buffer Data: 「 . $buffer . 「</p>」);
}
}
echo(「<p>Writing to Socket</p>」);
// Write some test data to our socket
if(!socket_write($socket, 「SOME DATA/r/n」))
{
echo(「<p>Write failed</p>」);
}
// Read any response from the socket
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>」);
?>

這個例子的代碼演示了客戶端連接到伺服器。客戶端讀取數據。如果這是第一時間到達這個循環的首次連接,這個伺服器將發送「NO DATA」返回給客戶端。如果情況發生了,這個客戶端在連接之上。客戶端發送它的數據到伺服器,數據發送給伺服器,客戶端等待響應。一旦接受到響應,那麼它將把響應寫到屏幕上。

C. PHP如何開啟socket

如果你是windows系統,請進入安裝目錄,找到php.ini這個文件,找到下面一行。
找到extension=php_sockets.dll一句,將前面的;去掉
;是注釋的意思。

如果你的Linux系統,重新編譯,./configure --help,自己看去,相信你懂的,你也必須學著看這個了。想看看自己的php是否有這個拓展,使用php -m命令來看,如果有,就不用重新編譯了。
--enable-sockets

D. php要讓伺服器使用socket要怎麼配置

socket伺服器的工作方式是這樣的,不間斷地運行以等待客戶端的連接。一旦客戶端連接上了,伺服器就會將它添加到客戶名單中,然後開始等待來自客戶端的消息。

下面是完整的源代碼:

// Set time limit to indefinite execution

set_time_limit (0);

// Set the ip and port we will listen on

$address = 'localhost';

$port = 10000;

$max_clients = 10;

// Array that will hold client information

$client = Array();

// Create a TCP Stream socket

$sock = socket_create(AF_INET, SOCK_STREAM, 0);

// Bind the socket to an address/port

socket_bind($sock, $address, $port) or die('Could not bind to address');

// Start listening for connections

socket_listen($sock);

echo "Waiting for connections... ";

// Loop continuously

while (true) {

// Setup clients listen socket for reading

$read[0] = $sock;

for ($i = 0; $i < $max_clients; $i++) {

if (isset($client[$i]['sock']))

$read[$i + 1] = $client[$i]['sock'];

}

// Set up a blocking call to socket_select()

if (socket_select($read, $write = NULL, $except = NULL, $tv_sec = 5) < 1)

continue;

/* if a new connection is being made add it to the client array */

if (in_array($sock, $read)) {

for ($i = 0; $i < $max_clients; $i++) {

if (empty($client[$i]['sock'])) {

$client[$i]['sock'] = socket_accept($sock);

echo "New client connected $i ";

break;

}

elseif ($i == $max_clients - 1)

echo "Too many clients... ";

}

} // end if in_array

// If a client is trying to write - handle it now

for ($i = 0; $i < $max_clients; $i++) { // for each client

if (isset($client[$i]['sock'])) {

if (in_array($client[$i]['sock'], $read)) {

$input = socket_read($client[$i]['sock'], 1024);

if ($input == null) {

echo "Client disconnecting $i ";

// Zero length string meaning disconnected

unset($client[$i]);

} else {

echo "New input received $i ";

// send it to the other clients

for ($j = 0; $j < $max_clients; $j++) {

if (isset($client[$j]['sock']) && $j != $i) {

echo "Writing '$input' to client $j ";

socket_write($client[$j]['sock'], $input, strlen($input));

}

}

if ($input == 'exit') {

// requested disconnect

socket_close($client[$i]['sock']);

}

}

} else {

echo "Client disconnected $i ";

// Close the socket

socket_close($client[$i]['sock']);

unset($client[$i]);

}

}

}

} // end while

// Close the master sockets

socket_close($sock);

可以先將它分解為幾個較小的部分。

  1. 第一部分是創建伺服器。Lines:2至20。

  2. 這部分代碼設置了變數、地址、埠、最大客戶端和客戶端數組。接下來創建socket並將其綁定到我們指定的地址和埠上。

  3. 下面我們要做的事情就是執行一個死循環(實際上我們是故意的!)。Lines:22至32。

  4. 在這部分代碼中我們做的第一步是設置 $read 數組。此數 組包含所有客戶端的套接字和我們主伺服器的套接字。這個變數稍後會用於select語句:告訴PHP監聽來自這些客戶端的每一條消息。

  5. socket_select()的最後一個參數告訴我們的伺服器在返回值之前最多等待5秒鍾。如果它的返回值小於1,那麼就表示沒有收到任何數據,所以只需要返回循環頂部,繼續等待。

腳本的下一個部分,是增加新的客戶端到數組中。Lines:33至44。

將新的客戶端放置在列表的末尾。檢查以確保客戶端的數量沒有超過我們想要伺服器處理的數量。

下面要介紹的代碼塊相當大,也是伺服器的主要部分。當客戶端將消息發送到伺服器時,就需要這塊代碼挺身而出來處理。消息可以是各種各樣的,斷開消息、實際斷開——只要是伺服器需要處理的消息。Lines:46至末尾。

代碼循環通過每個客戶端並檢查是否收到來自於它們的消息。如果是,獲取輸入的內容。根據輸入來檢查這是否是一個斷開消息,如果是那就從數組中刪除它們,反之,那它就是一個正常的消息,那我們的伺服器再次通過所有客戶端,並一個一個寫信息給他們,跳過發送者。

E. PHP socket重連機制實現

";
}
fclose($conn);
}
function WriteData($conn,$host,$data)
{
$header = "POST /test.php HTTP/1.1\r\n";
$header.= "Host : {$host}\r\n";
$header.= "Content-type: application/x-www-form-urlencoded\r\n";
$header.= "Content-Length:".strlen($data)."\r\n";
//Keep-Alive是關鍵
$header.= "Connection: Keep-Alive\r\n\r\n";
$header.= "{$data}\r\n\r\n";
fwrite($conn,$header);
//取結果
//$result = '';
//while(!feof($conn))
//{
// $result .= fgets($conn,128);
//}
//return $result;
}
Post('127.0.0.1',80);
?>

F. 如何查看php進程請求的socket詳情

PHP使用socket發送HTTP請求的方法,具體如下:
socket方式:
$socket=socket_create(AF_INET,SOCK_STREAM,SOL_TCP);//socket_set_option($socket,SOL_SOCKET,SO_SNDTIMEO,array("sec"=>20,"usec"=>0));socket_connect($socket,'www..com',80);//裡面的換行代表 注意拷貝的代碼後面可能有空格$http=<<<eof
GET/HTTP/1.0
Accept:*/*
User-Agent:Lowell-Agent
Host:www..com
Connection:Close
eof;
socket_write($socket,$http,strlen($http));while($str=socket_read($socket,1024))
{
echo$str;
}
socket_close($socket);
fsockopen方式:
$fp=fsockopen("www..com",80,$errno,$errstr,30);if(!$fp){
echo"$errstr($errno)<br/> ";
}else{
$out="GET/HTTP/1.1 ";
$out.="Host:www..com ";
$out.="Connection:Close ";
fwrite($fp,$http);
while(!feof($fp)){
echofgets($fp,128);
}
fclose($fp);
}
原始socket方式:
$fp=stream_socket_client("tcp://www..com:80",$errno,$errstr,30);if(!$fp){
echo"$errstr($errno)<br/> ";
}else{
$http=<<<eof
GET/HTTP/1.0
Accept:*/*
User-Agent:Lowell-Agent
Host:www..com
Connection:Close
eof;
fwrite($fp,$http);
while(!feof($fp)){
echofgets($fp,1024);
}
fclose($fp);
}
stream方式(get):
$http=<<<eof
Host:www..com
User-Agent:Mozilla/5.0(WindowsNT6.1;WOW64;rv:27.0)Gecko/20100101Firefox/27.0Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8Accept-Language:zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3Cookie:BAIDUID=:FG=1;BDUSS=lF--FPnkfhTU;BAIDUPSID=;BD_UPN=13314352;BD_HOME=1;H_PS_PSSID=10047_1435_10874_10212_10501_10496_10753_10796_10219_10355_10666_10597_10095_10658_10442_10700_10460_10360_10618;sug=3;sugstore=0;ORIGIN=2;bdime=0Connection:keep-alive
Cache-Control:max-age=0
eof;
$hdrs=array(
'http'=>array(
'header'=>$http,
'timeout'=>1,//超時秒
'method'=>'GET',//默認方式
'protocol_version'=>'1.1',//默認為1.0),
);
//參數格式參考http://php.net/manual/zh/context.http.php//curl方式的格式可以參考;http://php.net/manual/zh/context.curl.php$context=stream_context_create($hdrs);
echofile_get_contents('http://www..com',0,$context);stream方式post:
$postdata=http_build_query(array('act'=>'save','id'=>387171));$http=<<<eof
Host:www..com
User-Agent:Mozilla/5.0(WindowsNT6.1;WOW64;rv:27.0)Gecko/20100101Firefox/27.0Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8Accept-Language:zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3Content-Type:application/x-www-form-urlencoded;charset=UTF-8Cookie:BAIDUID=:FG=1;BDUSS=lF--FPnkfhTU;BAIDUPSID=;BD_UPN=13314352;BD_HOME=1;H_PS_PSSID=10047_1435_10874_10212_10501_10496_10753_10796_10219_10355_10666_10597_10095_10658_10442_10700_10460_10360_10618;sug=3;sugstore=0;ORIGIN=2;bdime=0Connection:keep-alive
Cache-Control:max-age=0
eof;
#注意post方式需要增加Content-Type
$hdrs=array(
'http'=>array(
'header'=>$http,
'timeout'=>1,//超時秒
'method'=>'POST',
'content'=>$postdata,
'protocol_version'=>'1.1',//默認為1.0),
);
//參數格式參考http://php.net/manual/zh/context.http.php//curl方式的格式可以參考;http://php.net/manual/zh/context.curl.php$context=stream_context_create($hdrs);
echofile_get_contents('http://test.cm/song.php',0,$context);

G. 如何用PHP實現Socket伺服器

我覺得只有調用了accept客戶端才能連上,你如果accept下面接一個循環,那麼第二個用戶不是說能不能進這個循環,而是連不進……此時只有第一個用戶break出了循環,然後程序回頭再調用accept,...如果還有不明白的話可以問我,我現在在後盾網學習馬上就上課了,希望能幫到你(๑•॒̀ ູ॒•́๑)啦啦啦

H. php socket在linux下如何運行

下個相同版本的php源碼,進行編譯安裝,再按照上面步驟搞,生成的so。到rpm裝的那個,修改php.ini進行擴展就行了,

或者到網上找相同版本,相同系統 的編譯好的so文件。

在linux下給PHP安裝socket擴展,參考方法如下:

#cd /usr/soft/php/ext/sockets (進入原php安裝文件下的sockets目錄)
#/usr/local/php/bin/phpize (運行安裝後的php安裝文件下的phpize)
#./configure --prefix=/usr/local/php/lib --with-php-config=/user/local/php/bin/php-config --enable-sockets
#make
#make install
再修改/usr/local/php/etc/php.ini文件
#extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/" (php5.4版本以上不用加擴展路徑)

extension=sockets.so

(若不想修改extension_dir路徑或改路徑下還有別的.so文件,亦可直接寫:

extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/sockets.so)

重啟apache或者nginx等進程,完成。

以上這篇linux下開啟php的sockets擴展支持實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

I. PHP SOCKET 技術研究

今天試著寫一個
PHP

C
語言通過socket通訊的程序,看過PHP手冊,發現有好幾種方式可以建立socket
客戶端.
1、通過
fsockopen()
建立socket連接,然後用
用fputs()
發送消息,用
fgets()
接收消息。
2、通過
socket_create()
建立
socket
連接,然後用
socket_send()
or
socket_write()
發送消息,用
socket_recv()
or
socket_read()
發送消息。

很奇怪,我在手冊上看到了這樣一段話"本擴展模塊是實驗性的。該模塊的行為,包括其函數的名稱以及其它任何關於此模塊的文檔可能會在沒有通知的情況下隨
PHP
以後的發布而改變。我們提醒您在使用本擴展模塊的同時自擔風險。"
看來
php4.0
socket通訊還不是完全穩定。
今天我寫的客戶端要與服務端做兩次通訊,我用上面這個方法都寫了一個客戶端程序,發現當僅僅就一次通訊的時候,也就是PHP客戶端發送一次消息,然後接收返回消息,就關閉連接。這兩種方法都能正確快速的實現功能,但當做兩次通訊時,卻有明顯的差別,第一種方法第一次通訊特別快就結束了,這個我可以通過服務端的輸出看出來,但是第二次通訊要等上好幾分鍾才能結束,我試了好幾次都這樣,我不太清楚我的程序哪裡出錯了,還是這個方式連接就是有問題,但是第二種方法做這兩次通訊卻很快,正確!完成的非常的。
最後我根據
第二種情況寫了一個
class
//////////////////////////////
File
Description
//////////////////////////////////////////
//
Class
Name
:
socket
//
Version
:
V1.0
//
Functional
Outline
:
create
socket,and
send
message
to
server
//
Revision
history
:
2004/12/15
First
version
created
//
Current
:
2004/12/15
Liu
Yongsheng
//////////////////////////////////////////////////////////////////////////////////////////
class
socket{
var
$socket;
//socket
句柄
var
$sendflag
=
">>>";
var
$recvflag
=
"<<<";
var
$response;
var
$debug
=
1;
function
socket($hostname,$port){

$address
=
gethostbyname($hostname);

$this->socket
=
socket_create(AF_INET,SOCK_STREAM,SOL_TCP);

$result
=
socket_connect($this->socket,$address,$port);

if($this->debug
==
1){

if
($result
<
0)
{

echo
"socket_connect()
failed.\nReason:
($result)
"
.
socket_strerror($result)
.
"<br>";

}
else{

echo
"connect
OK.<br>";

}

}
}
function
sendmsg($msg){

socket_write($this->socket,$msg,strlen($msg));

$result
=
socket_read($this->socket,100);

$this->response
=
$result;

if($this->debug
==
1){

printf("<font
color=#CCCCCC>%s
$msg</fon><br>",$this->sendflag);

printf("<font
color=blue>%s
$result</font><br>",$this->recvflag);

}

return
$result;
}
function
close(){

socket_close($this->socket);
}
}

J. PHP SOCKET,

也許這與PHP無關,TCP/IP協議本就有包大小的限制

熱點內容
db2新建資料庫 發布:2024-09-08 08:10:19 瀏覽:170
頻率計源碼 發布:2024-09-08 07:40:26 瀏覽:778
奧迪a6哪個配置帶後排加熱 發布:2024-09-08 07:06:32 瀏覽:100
linux修改apache埠 發布:2024-09-08 07:05:49 瀏覽:208
有多少個不同的密碼子 發布:2024-09-08 07:00:46 瀏覽:566
linux搭建mysql伺服器配置 發布:2024-09-08 06:50:02 瀏覽:995
加上www不能訪問 發布:2024-09-08 06:39:52 瀏覽:811
銀行支付密碼器怎麼用 發布:2024-09-08 06:39:52 瀏覽:513
蘋果手機清理瀏覽器緩存怎麼清理緩存 發布:2024-09-08 06:31:32 瀏覽:554
雲伺服器的優點與缺點 發布:2024-09-08 06:30:34 瀏覽:734