php里使用SOCKET通信

来源:互联网 发布:imac装windows系统 编辑:程序博客网 时间:2024/06/05 00:13
class socketClient{    public $socketConnection;    public $Connection;    public $changed_sockets;        public function __construct($ipAddress,$port){        $this->socketConnection = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);        $this->Connection = socket_connect($this->socketConnection,$ipAddress, $port);        $this->changed_sockets = array($this->socketConnection);    }        public function sendMsg($msg){        socket_write($this->socketConnection, $msg);    }    public function recv(){        $num_changed_sockets = socket_select($this->changed_sockets, $write = NULL, $except = NULL, NULL);        foreach($this->changed_sockets as $socket) {            $bytes = @socket_recv($this->socketConnection, $buffer, 2048, 0);            return $buffer;        }    }    public function close(){        socket_close($this->socketConnection);    }}


 

原创粉丝点击