socket用戶端-php socket 用戶端的多次通訊

來源:互聯網
上載者:User
關鍵字 socket用戶端 socket js php
socket用戶端socketjsphp

想用php做一個socket 用戶端,可以實現一次串連,然後多次給服務端發訊息。具體的想法是這樣的:
做一個頁面,這個頁面上有個按鈕和訊息列,點一下按鈕就可以把訊息列的內容發送給服務端,再點一下再發送,而不需要重新串連,只是頁面開啟時跟服務端串連上。
這個要怎麼實現呀?請各位大牛幫幫忙,小妹先謝過了。
附上socket類的代碼。

/** 定義ip,和連接埠 */

define ( 'PHP_SOCKET_PORT', '6000' );
define ( 'PHP_SOCKET_HOST', '127.0.0.1' );
define ( 'PHP_SOCKET_START', '' );
define ( 'PHP_SOCKET_END', '' );
// socket class
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) {
$result = "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "
";
} else{
$result = "connect OK.
";
}
}

}
function sendmsg($msg){
socket_write($this->socket,$msg,strlen($msg));
$result = socket_read($this->socket,100);
$this->response = $result;
if($this->debug == 1){
printf("%s $msg
",$this->sendflag);
printf("%s $result
",$this->recvflag);
}
return $result;
}
function close(){
socket_close($this->socket);
}
}
?>

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.