php中Socket创建与监听实现方法
这篇文章主要介绍了php中Socket创建与监听实现方法,以实例形式较为详细的分析了Socket的创建、连接、初始化及监听等技巧,需要的朋友可以参考下
本文实例讲述了php中Socket创建与监听实现方法。分享给大家供大家参考。具体如下:
这里提供了在php中socket相关函数的一个实例,如创建Socket,接受一个socket连接 ,写入一个Socket及初始化连接等操作,具体实例代码如下:
复制代码 代码如下:
$ip="127.0.0.1"; //定义ip地址
$port=1000; //定义端口
$socket=socket_create(af_inet,sock_stream,sol_tcp); //创建socket
$bind=socket_bind($socket,$ip,$prot); //绑定一个socket
$listen=socket_listen($socket); //监听socket
$msg=socket_accept($socket); //接受一个socket连接
$welcome="欢迎来到php服务/n"; //定义字符串
socket_write($msg,$welcome,strlen($welcome)); //写入一个socket
socket_close($socket); //关闭socket
*/
$ip="127.0.0.1"; //定义ip地址
$port=1000; //定义端口
$socket=socket_create(af_inet,sock_stream,sol_tcp); //创建socket
socket_clear_error($socket); //清空socket错误信息
socket_close($socket); //关闭socket
/*
*/
$sockets=array(); //定义一个数组
/*建立一对socket,并存储在数组中*/
if(!socket_create_pair(af_unix,sock_stream,0,$sockets)) //如果socket没有成功创建
{
echo socket_strerror(socket_last_error()); //输出错误信息
}
/*使用创建的socket收发信息*/
if(!socket_write($sockets[0],"abcdef123n",strlen("abcdef123n"))) //如果执行socket写失败
{
cho socket_strerror(socket_last_error()); //输出错误信息
}
if(!$data=socket_read($sockets[1],strlen("abcdef123n"),php_binary_read)) //如果读取信息失败
{
echo socket_strerror(socket_last_error()); //输出错误信息
}
var_dump($data); //输出socket数据信息
/*关闭socket*/
socket_close($sockets[0]); //关闭socket1
socket_close($sockets[1]); //关闭socket2
/*
*/
$ip="127.0.0.1"; //定义ip地址
$port=1000; //定义端口
$socket=socket_create(af_inet,sock_stream,sol_tcp); //创建socket
socket_set_nonblock($socket); //设置不锁定状态
socket_connect($socket,$ip,$port); //初始化连接
socket_set_block($sock); //锁定socket
switch(socket_select($r=array($socket),$w=array($socket),$f=array($socket),5))
{
case 2:
echo "连接被拒绝!n";
break;
case 1:
echo "成功连接n";
break;
case 0:
echo "连接超时n";
break;
}
/*
*/
/*以下代码定义一个socket监听类*/
class socketlistener extends socket
{
function __construct($port=null) //定义__construct方法
{
$this->stream=@socket_create_listen($port?$port:port); //打开端口接受连接
}
function isactive() //定义isactive方法
{
if($this->stream) //如果有连接
{
return true; //返回真值
}
else //无连接
{
return false; //返回假值
}
}
}
希望本文所述对大家的php程序设计有所帮助。
,
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

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

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



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

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
