這篇文章給大家介紹socket怎么在php中使用,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
一、開(kāi)啟socket
phpinfo();查看是否開(kāi)啟了socket擴(kuò)展,否則在php.ini中開(kāi)啟。
二、服務(wù)器端代碼的寫(xiě)法
<?phperror_reporting(E_ALL);set_time_limit(0);//ob_implicit_flush(); $address = '127.0.0.1';$port = 10005;//創(chuàng)建端口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";} //監(jiān)聽(tīng)if (socket_listen($sock, 5) === false) { echo "socket_bind() failed :reason:" . socket_strerror(socket_last_error($sock)) . "\n";} do { //得到一個(gè)鏈接 if (($msgsock = socket_accept($sock)) === false) { echo "socket_accepty() failed :reason:".socket_strerror(socket_last_error($sock)) . "\n"; break; } //welcome 發(fā)送到客戶(hù)端 $msg = "<font color='red'>server send:welcome</font><br/>"; socket_write($msgsock, $msg, strlen($msg)); echo 'read client message\n'; $buf = socket_read($msgsock, 8192); $talkback = "received message:$buf\n"; echo $talkback; if (false === socket_write($msgsock, $talkback, strlen($talkback))) { echo "socket_write() failed reason:" . socket_strerror(socket_last_error($sock)) ."\n"; } else { echo 'send success'; } socket_close($msgsock);} while(true);//關(guān)閉socketsocket_close($sock);?>
服務(wù)器端需要在cli模式是執(zhí)行,有可能cli模式下php.ini文件載入的不一樣
可以像如下輸出:
這時(shí)候在zhoxh目錄下就有個(gè)tem.text文件。查看 Configuration File (php.ini) Path => C:\WINDOWS 。不是我的php.ini 文件,這說(shuō)明調(diào)用的php.ini文件時(shí)錯(cuò)誤的。這時(shí)候我們要指定php.ini文件命令如下
注意的是我的php可以直接執(zhí)行時(shí)配置了環(huán)境變量。
三、客戶(hù)端
<?php
//error_reporting(E_ALL);
echo "<h3>tcp/ip connection </h3>\n";
$service_port = 10005;
$address = '127.0.0.1';
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
echo "OK. \n";
}
echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if($result === false) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
echo "OK \n";
}
$in = "HEAD / http/1.1\r\n";
$in .= "HOST: localhost \r\n";
$in .= "Connection: close\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, 8192)) {
echo $out;
}
echo "closeing socket..";
socket_close($socket);
echo "ok .\n\n";
執(zhí)行結(jié)果如下:
server:
client:
關(guān)于socket怎么在php中使用就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
文章題目:socket怎么在php中使用-創(chuàng)新互聯(lián)
瀏覽路徑:http://jinyejixie.com/article28/dsiocp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷(xiāo)推廣、微信公眾號(hào)、自適應(yīng)網(wǎng)站、App設(shè)計(jì)、企業(yè)建站、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容