php中socket通信机制实例详解
这篇文章主要介绍了php中socket通信机制,讲述了socket通信机制的原理,并以实例形式较为详细的分析了socket通信机制的用法,需要的朋友可以参考下
本文实例讲述了php中socket通信机制及用法。分享给大家供大家参考。具体分析如下:
一、socket是什么
什么是socket 所谓socket通常也称作"套接字",用于描述ip地址和端口,是一个通信链的句柄。应用程序通常通过"套接字"向网络发出请求或者应答网络请求。说白了就是一种通信机制。它类似于银行,电信啊这些部分的电话客服部门。你打电话的时候,那边会分配置一个人回答你的问题,,客服部门就相当于socket的服务器端了,你这边呢就相当于客户端了,在和你通话结束前,如果有人在想找和你通话的那个说话,是不可能的,因为你在和他通信,当然客服部门的电话交换机也不会重复分配。
下面我将举例子来说明一下,socket是怎么工作的。如果你是基于应用层开发的人员并不一定要理解原理,但是能知道那是更好了。网上有关于socket的php api。下下来用就行了。
二、socket服务器server.php
复制代码 代码如下:
// 建立server端socket
$tcp = getprotobyname("tcp");
$socket = socket_create(af_inet, sock_stream, $tcp);
socket_bind($socket, '127.0.0.1', 10008); //绑定要监听的端口
socket_listen($socket); //监听端口
//初始化一个数据,和客户端通信
$buffer = "connect";
while (true) {
// 接受一个socket连接
$connection = socket_accept($socket);
if(!$connection){
echo "connect fail";
}else{
echo "socket connected ";
// 向客户端传递一个信息数据
if ($buffer != "") {
echo "send data to client ";
socket_write($connection, $buffer . " ");
echo "wrote to socket ";
} else {
echo "no data in the buffer " ;
}
// 从客户端取得信息
while ($data = @socket_read($connection, 1024, php_normal_read)) {
printf("buffer: " . $data . " ");
//取得信息给客户端一个反馈
socket_write($connection, "information received ");
}
}
socket_close($connection);
//关闭 socket
printf("closed the socket ");
}
?>
三、socket客户端client.php
复制代码 代码如下:
// 建立客户端的socet连接
$socket = socket_create(af_inet, sock_stream, sol_tcp);
$connection = socket_connect($socket, '127.0.0.1', 10008); //连接服务器端socket
while ($buffer = @socket_read($socket, 1024, php_normal_read)) {
//服务端告诉客户端,自己的状态
if (preg_match("/not connect/",$buffer)) {
echo "don`t connect ";
break;
} else {
//服务器传来信息
echo "buffer data: " . $buffer . " ";
echo "writing to socket ";
// 将客户的信息写到通道中,传给服务器端
if (!socket_write($socket, "some data ")) {
echo "write failed ";
}
//服务器端收到信息后,给于的回应信息
while ($buffer = socket_read($socket, 1024, php_normal_read)) {
echo "sent to server: some data response from server was:" . $buffer . " ";
}
}
}
?>
四、通信机制的一个图片(不考虑等待时间结束自动关闭socket)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

Validator can be created by adding the following two lines in the controller.
