Home Backend Development PHP Tutorial phpsocket客户端以及服务器例证

phpsocket客户端以及服务器例证

Jun 13, 2016 pm 12:00 PM
client function quot socket this

phpsocket客户端以及服务器例子

一个菜鸟朋友,突然问了我这个问题...现在稍稍有点时间,就写了一个简单的例子给他,顺便贴上来

服务器端:

<?php /** * @author 邹颢	[email&#160;protected] */class SocketServer{	private $_port=&#39;9000&#39;;	private $_address=&#39;127.0.0.1&#39;;	private $_client_socket_list=array();	public function __set($name,$val){		$this->$name=$val;	}	private function _showError($error){		exit($error);	}	/**	 * 开始进行socket服务器端监听端口	 */	public function start(){		// 创建端口		if (($sock = socket_create ( AF_INET, SOCK_STREAM, SOL_TCP )) === false) {			$this-&gt;_showError("socket_create() failed :reason:" . socket_strerror ( socket_last_error () ));		}		// 绑定		if (socket_bind ( $sock, $this-&gt;_address, $this-&gt;_port ) === false) {			$this-&gt;_showError("socket_bind() failed :reason:" . socket_strerror ( socket_last_error ( $sock ) ));		}		// 监听		if (socket_listen ( $sock, 5 ) === false) {			$this-&gt;_showError("socket_bind() failed :reason:" . socket_strerror ( socket_last_error ( $sock ) ) );		}		do {			//当有一个客户端连接的时候			if ($client_socket=socket_accept ( $sock )) {				$count = count ( $this-&gt;_client_socket_list ) + 1;				//把新来的用户加入 客户端数组里				$this-&gt;_client_socket_list[]=$client_socket;				echo "new connection:\r\n";//服务器端输出当前正在连接的客户端数量				echo "current connection:{$count}\r\n";				//接受客户端传过来的字符串				$msg=$this-&gt;read($client_socket);				echo "client:{$msg}\r\n";				//服务器向客户端传值				$my_msg="I am fine,think you\r\n";				$this-&gt;send($client_socket,$my_msg);			}			/**			 * 这段代码给你参考,用来判断是否有客户端主动失去连接			else{				foreach ( $this-&gt;_client_socket_list as $socket ) {					$len = socket_recv ($socket, $buffer, 2048, 0 ); // 接受一下客户端信息,如果为0代表断开连接					if ($len start();//开始监听
Copy after login
Copy after login

客户端:

<?php /** * @author 邹颢	[email&#160;protected] */class SocketServer{	private $_port=&#39;9000&#39;;	private $_address=&#39;127.0.0.1&#39;;	private $_client_socket_list=array();	public function __set($name,$val){		$this->$name=$val;	}	private function _showError($error){		exit($error);	}	/**	 * 开始进行socket服务器端监听端口	 */	public function start(){		// 创建端口		if (($sock = socket_create ( AF_INET, SOCK_STREAM, SOL_TCP )) === false) {			$this-&gt;_showError("socket_create() failed :reason:" . socket_strerror ( socket_last_error () ));		}		// 绑定		if (socket_bind ( $sock, $this-&gt;_address, $this-&gt;_port ) === false) {			$this-&gt;_showError("socket_bind() failed :reason:" . socket_strerror ( socket_last_error ( $sock ) ));		}		// 监听		if (socket_listen ( $sock, 5 ) === false) {			$this-&gt;_showError("socket_bind() failed :reason:" . socket_strerror ( socket_last_error ( $sock ) ) );		}		do {			//当有一个客户端连接的时候			if ($client_socket=socket_accept ( $sock )) {				$count = count ( $this-&gt;_client_socket_list ) + 1;				//把新来的用户加入 客户端数组里				$this-&gt;_client_socket_list[]=$client_socket;				echo "new connection:\r\n";//服务器端输出当前正在连接的客户端数量				echo "current connection:{$count}\r\n";				//接受客户端传过来的字符串				$msg=$this-&gt;read($client_socket);				echo "client:{$msg}\r\n";				//服务器向客户端传值				$my_msg="I am fine,think you\r\n";				$this-&gt;send($client_socket,$my_msg);			}			/**			 * 这段代码给你参考,用来判断是否有客户端主动失去连接			else{				foreach ( $this-&gt;_client_socket_list as $socket ) {					$len = socket_recv ($socket, $buffer, 2048, 0 ); // 接受一下客户端信息,如果为0代表断开连接					if ($len start();//开始监听
Copy after login
Copy after login

注意事项:服务器端请用CLI模式运行,cgi模式会超时,新手常喜欢犯的错误.什么是CLI模式,简单的说就是用命令行去执行,而不要用游览器打开,否则会超时的
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

What does function mean?

How to use Python's socket and socketserver How to use Python's socket and socketserver May 28, 2023 pm 08:10 PM

How to use Python's socket and socketserver

How to use Spring Boot+Vue to implement Socket notification push How to use Spring Boot+Vue to implement Socket notification push May 27, 2023 am 08:47 AM

How to use Spring Boot+Vue to implement Socket notification push

What to do if php socket cannot connect What to do if php socket cannot connect Nov 09, 2022 am 10:34 AM

What to do if php socket cannot connect

IO multiplexing of PHP+Socket series and implementation of web server IO multiplexing of PHP+Socket series and implementation of web server Feb 02, 2023 pm 01:43 PM

IO multiplexing of PHP+Socket series and implementation of web server

PHP+Socket series realizes data transmission between client and server PHP+Socket series realizes data transmission between client and server Feb 02, 2023 am 11:35 AM

PHP+Socket series realizes data transmission between client and server

Common network communication and security problems and solutions in C# Common network communication and security problems and solutions in C# Oct 09, 2023 pm 09:21 PM

Common network communication and security problems and solutions in C#

Research on real-time file transfer technology using PHP and Socket Research on real-time file transfer technology using PHP and Socket Jun 28, 2023 am 09:11 AM

Research on real-time file transfer technology using PHP and Socket

See all articles