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->_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 "new connection:\r\n";//服务器端输出当前正在连接的客户端数量				echo "current connection:{$count}\r\n";				//接受客户端传过来的字符串				$msg=$this->read($client_socket);				echo "client:{$msg}\r\n";				//服务器向客户端传值				$my_msg="I am fine,think you\r\n";				$this->send($client_socket,$my_msg);			}			/**			 * 这段代码给你参考,用来判断是否有客户端主动失去连接			else{				foreach ( $this->_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->_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 "new connection:\r\n";//服务器端输出当前正在连接的客户端数量				echo "current connection:{$count}\r\n";				//接受客户端传过来的字符串				$msg=$this->read($client_socket);				echo "client:{$msg}\r\n";				//服务器向客户端传值				$my_msg="I am fine,think you\r\n";				$this->send($client_socket,$my_msg);			}			/**			 * 这段代码给你参考,用来判断是否有客户端主动失去连接			else{				foreach ( $this->_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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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)

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

This article brings you relevant knowledge about php+socket, which mainly introduces IO multiplexing and how php+socket implements web server? Friends who are interested can take a look below. I hope it will be helpful to everyone.

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

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

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

1. Socket programming based on TCP protocol 1. The socket workflow starts with the server side. The server first initializes the Socket, then binds to the port, listens to the port, calls accept to block, and waits for the client to connect. At this time, if a client initializes a Socket and then connects to the server (connect), if the connection is successful, the connection between the client and the server is established. The client sends a data request, the server receives the request and processes the request, then sends the response data to the client, the client reads the data, and finally closes the connection. An interaction ends. Use the following Python code to implement it: importso

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

The first step on the SpringBoot side is to introduce dependencies. First we need to introduce the dependencies required for WebSocket, as well as the dependencies for processing the output format com.alibabafastjson1.2.73org.springframework.bootspring-boot-starter-websocket. The second step is to create the WebSocket configuration class importorg. springframework.context.annotation.Bean;importorg.springframework.context.annotation.Config

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

Solution to the problem that the php socket cannot be connected: 1. Check whether the socket extension is enabled in php; 2. Open the php.ini file and check whether "php_sockets.dll" is loaded; 3. Uncomment "php_sockets.dll".

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# In today's Internet era, network communication has become an indispensable part of software development. In C#, we usually encounter some network communication problems, such as data transmission security, network connection stability, etc. This article will discuss in detail common network communication and security issues in C# and provide corresponding solutions and code examples. 1. Network communication problems Network connection interruption: During the network communication process, the network connection may be interrupted, which may cause

Methods and techniques for implementing Socket communication in PHP Methods and techniques for implementing Socket communication in PHP Mar 07, 2024 pm 02:06 PM

PHP is a commonly used development language that can be used to develop various web applications. In addition to common HTTP requests and responses, PHP also supports network communication through Sockets to achieve more flexible and efficient data interaction. This article will introduce the methods and techniques of how to implement Socket communication in PHP, and attach specific code examples. What is Socket Communication Socket is a method of communication in a network that can transfer data between different computers. by S

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

This article brings you relevant knowledge about php+socket. It mainly introduces what is socket? How does php+socket realize client-server data transmission? Friends who are interested can take a look below. I hope it will be helpful to everyone.

See all articles