search
HomeBackend DevelopmentPHP TutorialPHP的Socket网络编程入门指引_PHP

什么是TCP/IP、UDP?

         TCP/IP(Transmission Control Protocol/Internet Protocol)即传输控制协议/网间协议,是一个工业标准的协议集,它是为广域网(WANs)设计的。
         UDP(User Data Protocol,用户数据报协议)是与TCP相对应的协议。它是属于TCP/IP协议族中的一种。
        这里有一张图,表明了这些协议的关系。

2015811151417312.jpg (596×448)

TCP/IP协议族包括运输层、网络层、链路层。现在你知道TCP/IP与UDP的关系了吧。
Socket在哪里呢?
  在图1中,我们没有看到Socket的影子,那么它到底在哪里呢?还是用图来说话,一目了然。

2015811151446490.jpg (542×476)

原来Socket在这里。
Socket是什么呢?
  Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口。在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Socket接口后面,对用户来说,一组简单的接口就是全部,让Socket去组织数据,以符合指定的协议。
你会使用它们吗?
  前人已经给我们做了好多的事了,网络间的通信也就简单了许多,但毕竟还是有挺多工作要做的。以前听到Socket编程,觉得它是比较高深的编程知识,但是只要弄清Socket编程的工作原理,神秘的面纱也就揭开了。
  一个生活中的场景。你要打电话给一个朋友,先拨号,朋友听到电话铃声后提起电话,这时你和你的朋友就建立起了连接,就可以讲话了。等交流结束,挂断电话结束此次交谈。 生活中的场景就解释了这工作原理,也许TCP/IP协议族就是诞生于生活中,这也不一定。

2015811151507191.jpg (478×491)

PHP的Socket编程概述
php5.3自带了socket模块,使得php具有socket通信能力,具体api可以参考官方手册:http://php.net/manual/zh/function.socket-create.php, 具体实现跟c非常类似,只是少了内存分配和网络字节序转换这种底层操作

同时,php的pcntl模块和posix模块配合可以实现基本的进程管理、信号处理等操作系统级别的功能。这里有两个非常关键的函数,pcntl_fork()和posix_setsid()。fork()一个进程,则表示创建了一个运行进程的副本,副本被认为是子进程,而原始进程被认为是父进程。当fork()运行之后,则可以脱离启动它的进程和终端控制等,也意味着父进程可以自由退出。pcntl_fork()返回值,-1表示执行失败,0表示在子进程中,大于0表示在父进程中。setsit(),它首先使新进程成为一个新会话的“领导者”,最后使进程不再控制终端。这也是成为守护进程最关键一步,这意味着,不会随着终端关闭而强制退出进程。对于一个不会被中断的常驻进程来说,这是很关键的一步。进行最后一次fork(),这一步不是必须的,但通常都这么做,它最大的意义是防止获得控制终端

什么是守护进程?一个守护进程通常被认为是一个不对终端进行控制的后台任务。它有三个很明显的特征:

  1.     在后台运行
  2.     与启动他的进程脱离
  3.     无须终端控制

最常见的实现方法:fork() -> setsid() -> fork(), 代码里run_server()方法实现了守护进程。

server端socket监听代码

  <&#63;php 
   
  // 接受客户端请求,回复固定的响应内容 
  function server_listen_socket ($address, $port) 
  { 
    $buffer = "Msg from wangzhengyi server, so kubi..."; 
    $len = strlen($buffer); 
     
    // create, bind and listen to socket 
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 
    if (! $socket) { 
      echo "failed to create socket:" . socket_strerror($socket) . "\n"; 
      exit(); 
    } 
     
    $bind_flag = socket_bind($socket, $address, $port); 
    if (! $bind_flag) { 
      echo "failed to bind socket:" . socket_strerror($bind_flag) . "\n"; 
      exit(); 
    } 
     
    $backlog = 20; 
    $listen_flag = socket_listen($socket, $backlog); 
    if (! $listen_flag) { 
      echo "failed to listen to socket:" . socket_strerror($listen_flag) . "\n"; 
      exit(); 
    } 
     
    echo "waiting for clients to connect\n"; 
     
    while (1) { 
      if (($accept_socket = socket_accept($socket)) == FALSE) { 
        continue; 
      } else { 
        socket_write($accept_socket, $buffer, $len); 
        socket_close($accept_socket); 
      } 
    } 
  } 
   
  function run_server () 
  { 
    $pid1 = pcntl_fork(); 
    if ($pid1 == 0) { 
      // first child process 
       
      // 守护进程的一般流程:fork()->setsid()->fork() 
      posix_setsid(); 
       
      if (($pid2 = pcntl_fork()) == 0) { 
        $address = "192.168.1.71"; 
        $port = "8767"; 
        server_listen_socket($address, $port); 
      } else { 
        // 防止获得控制终端 
        exit(); 
      } 
    } else { 
      // wait for first child process exit 
      pcntl_wait($status); 
    } 
  } 
   
  // server守护进程 
  run_server(); 

运行效果
启动服务器端socket进程,看是否在后台运行,效果如图:

2015811151526030.png (985×174)

客户端访问,可以通过浏览器或者curl访问,这里直接用curl访问了

2015811151634550.png (930×64)

Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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 Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool