Home Backend Development PHP Tutorial php中Socket创建与监听实现方法_php技巧

php中Socket创建与监听实现方法_php技巧

May 16, 2016 pm 08:26 PM
php socket create method monitor

本文实例讲述了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程序设计有所帮助。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

See all articles