php socket客户端及服务器端应用实例
经常有朋友会对php的socket应用充满疑惑,本文就以实例代码作一讲解,希望能对初学php的朋友起到一点帮助作用
具体代码如下:
1.服务器端代码:
<?php
classSocketServer{
private$_port='9000';
private$_address='127.0.0.1';
private$_client_socket_list=array();
publicfunction__set($name,$val){
$this--->$name=$val;
}
privatefunction_showError($error){
exit($error);
}
/**
*开始进行socket服务器端监听端口
*/
publicfunctionstart(){
//创建端口
if(($sock=socket_create(AF_INET,SOCK_STREAM,SOL_TCP))===false){
$this->_showError("socket_create()failed:reason:".socket_strerror(socket_last_error()));
}
//绑定
if(socket_bind($sock,$this->_address,$this->_port)===false){
$this->_showError("socket_bind()failed:reason:".socket_strerror(socket_last_error($sock)));
}
//监听
if(socket_listen($sock,5)===false){
$this->_showError("socket_bind()failed:reason:".socket_strerror(socket_last_error($sock)));
}
do{
//当有一个客户端连接的时候
if($client_socket=socket_accept($sock)){
$count=count($this->_client_socket_list)+1;
//把新来的用户加入客户端数组里
$this->_client_socket_list[]=$client_socket;
echo"newconnection:\r\n";//服务器端输出当前正在连接的客户端数量
echo"currentconnection:{$count}\r\n";
//接受客户端传过来的字符串
$msg=$this->read($client_socket);
echo"client:{$msg}\r\n";
//服务器向客户端传值
$my_msg="Iamfine,thinkyou\r\n";
$this->send($client_socket,$my_msg);
}
/**
*这段代码给你参考,用来判断是否有客户端主动失去连接
else{
foreach($this->_client_socket_listas$socket){
$len=socket_recv($socket,$buffer,2048,0);//接受一下客户端信息,如果为0代表断开连接
if($len<7){
//这里写是去连接的客户端业务
}
}
}
*/
}while(true);
}
/**
*发送数据给客户端
*/
publicfunctionsend($client_socket,$str){
returnsocket_write($client_socket,$str,strlen($str));
}
/**
*从客户端接受数据
*/
publicfunctionread($client_socket){
returnsocket_read($client_socket,8192);//8192实际代表的接受长度,我用819292表示长一点,这样长一点的字符串也可以接受到,不到8192也没关系,会自动识别
}
}
$socket_server=newSocketServer();
$socket_server->start();//开始监听
2.客户端代码:
<?php
classSocketServer{
private$_port='9000';
private$_address='127.0.0.1';
private$_client_socket_list=array();
publicfunction__set($name,$val){
$this--->$name=$val;
}
privatefunction_showError($error){
exit($error);
}
/**
*开始进行socket服务器端监听端口
*/
publicfunctionstart(){
//创建端口
if(($sock=socket_create(AF_INET,SOCK_STREAM,SOL_TCP))===false){
$this->_showError("socket_create()failed:reason:".socket_strerror(socket_last_error()));
}
//绑定
if(socket_bind($sock,$this->_address,$this->_port)===false){
$this->_showError("socket_bind()failed:reason:".socket_strerror(socket_last_error($sock)));
}
//监听
if(socket_listen($sock,5)===false){
$this->_showError("socket_bind()failed:reason:".socket_strerror(socket_last_error($sock)));
}
do{
//当有一个客户端连接的时候
if($client_socket=socket_accept($sock)){
$count=count($this->_client_socket_list)+1;
//把新来的用户加入客户端数组里
$this->_client_socket_list[]=$client_socket;
echo"newconnection:\r\n";//服务器端输出当前正在连接的客户端数量
echo"currentconnection:{$count}\r\n";
//接受客户端传过来的字符串
$msg=$this->read($client_socket);
echo"client:{$msg}\r\n";
//服务器向客户端传值
$my_msg="Iamfine,thinkyou\r\n";
$this->send($client_socket,$my_msg);
}
/**
*这段代码给你参考,用来判断是否有客户端主动失去连接
else{
foreach($this->_client_socket_listas$socket){
$len=socket_recv($socket,$buffer,2048,0);//接受一下客户端信息,如果为0代表断开连接
if($len<7){
//这里写是去连接的客户端业务
}
}
}
*/
}while(true);
}
/**
*发送数据给客户端
*/
publicfunctionsend($client_socket,$str){
returnsocket_write($client_socket,$str,strlen($str));
}
/**
*从客户端接受数据
*/
publicfunctionread($client_socket){
returnsocket_read($client_socket,8192);//8192实际代表的接受长度,我用819292表示长一点,这样长一点的字符串也可以接受到,不到8192也没关系,会自动识别
}
}
$socket_server=newSocketServer();
$socket_server->start();//开始监听
注意事项:服务器端请用CLI模式运行,cgi模式会超时,这是新手常喜欢犯的错误。那么什么是CLI模式呢?简单的说就是用命令行去执行,而不要用游览器打开,否则会超时的!