PHP SOCKET编程!
SERVER.PHP
代码
<p class="sycode"> <? php // 确保在连接客户端时不会超时 set_time_limit ( 0 ); // 设置IP和端口号 $address = 服务器自己的IP; $port = 端口号; // 创建一个SOCKET if (( $sock = socket_create(AF_INET , SOCK_STREAM , SOL_TCP)) < 0 ){ echo " socket_create() 失败的原因是: " . socket_strerror( $sock ) . " <br> " ;} // 绑定到socket端口 if (( $ret = socket_bind( $sock , $address , $port )) < 0 ){ echo " socket_bind() 失败的原因是: " . socket_strerror( $ret ) . " <br> " ;} // 开始监听 if (( $ret = socket_listen( $sock , 4 )) < 0 ){ echo " socket_listen() 失败的原因是: " . socket_strerror( $ret ) . " <br> " ;} do { if (( $msgsock = socket_accept( $sock )) < 0 ) { echo " socket_accept() failed: reason: " . socket_strerror( $msgsock ) . " \n " ; break ; } // 发到客户端 $msg = " <font color=red>服务器端发送:欢迎进入服务器!</font><br> " ; socket_write( $msgsock , $msg , strlen ( $msg )); echo " 读取客户端发来的信息<br> " ; $buf = socket_read( $msgsock , 8192 ); $talkback = " 收到的信息 $buf <br> " ; echo $talkback ; if (socket_write( $msgsock , $talkback , strlen ( $talkback )) < 0 ) { echo " socket_write() failed: reason: " . socket_strerror( $msgsock ) . " \n " ; } else { echo " 发送成功 " ; } echo $buf ; socket_close( $msgsock ); } while ( true ); socket_close( $sock ); ?> </p>
CLIENT.PHP
代码
<p class="sycode"> <? php error_reporting ( E_ALL ); set_time_limit ( 0 ); echo " <h2 id="TCP-IP-Connection">TCP/IP Connection</h2>\n " ; $service_port = 服务器端口; $address = " 服务器IP " ; $socket = socket_create(AF_INET , SOCK_STREAM , SOL_TCP); if ( $socket < 0 ){ echo " socket_create() failed: reason: " . socket_strerror( $socket ) . " \n " ;} else { echo " OK.\n " ;} echo " 试图连接 ' $address ' 端口 ' $service_port '...<br> " ; $result = socket_connect( $socket , $address , $service_port ); if ( $result < 0 ) { echo " socket_connect() failed.\nReason: ( $result ) " . socket_strerror( $result ) . " \n " ; } else { echo " 连接OK<br> " ; } $in = " 发送到服务器的信息\r\n " ; $in .= " 也是发到服务器的\r\n " ; $out = '' ; $out1 = '' ; if ( ! socket_write( $socket , $in , strlen ( $in ))) { echo " socket_write() failed: reason: " . socket_strerror( $socket ) . " \n " ; } else { echo " 发送到服务器信息成功!<br> " ; } while ( $out = socket_read( $socket , 8192 )) { echo " 接收服务器回传信息成功!<br> " ; echo $out ; } echo " 关闭SOCKET...<br> " ; socket_close( $socket ); echo " 关闭OK<br> " ; ?> </p>

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



Alipay PHP...

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,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

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.
