Home > Backend Development > PHP Tutorial > socket客户端-php socket 客户端的多次通信

socket客户端-php socket 客户端的多次通信

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-02 11:34:26
Original
1660 people have browsed it

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 $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);
}
}
?>

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template