php中Socket创建与监听实现方法_php技巧
本文实例讲述了php中Socket创建与监听实现方法。分享给大家供大家参考。具体如下:
这里提供了在php中socket相关函数的一个实例,如创建Socket,接受一个socket连接 ,写入一个Socket及初始化连接等操作,具体实例代码如下:
$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

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



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

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

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.

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

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

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

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
