


Detailed introduction based on PHP Socket configuration and examples_PHP tutorial
2 php test files
server.php
//phpinfo();
//Ensure there will be no timeout when connecting to the client
set_time_limit(0);
$ip = '127.0.0.1 ';
$port = 1935;
/*
+----------------------------------
* The whole process of @socket communication
+----------------------------------
* @socket_create
* @socket_bind
* @socket_listen
* @socket_accept
* @socket_read
* @socket_write
* @socket_close
+---------- ----------------------------
*/
/*------------- The following operations It's all in the manual -------------------*/
if(($sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP)) < 0) {
echo "socket_create() failed because: ".socket_strerror($sock)."n";
}
if(($ret = socket_bind($sock,$ip,$port)) < ; 0) {
echo "socket_bind() failed because: ".socket_strerror($ret)."n";
}
if(($ret = socket_listen($sock,4)) < 0) {
echo "socket_listen() failed because: ".socket_strerror($ret)."n";
}
$count = 0;
do {
if (($msgsock = socket_accept($sock)) < 0) {
echo "socket_accept() failed: reason: " . socket_strerror($msgsock) "n";
else {
//Send to client
$msg="Test successful! n";
socket_write($msgsock, $msg, strlen($msg));
echo "The test was successfuln";
$buf = socket_read($msgsock,8192);
break;
};
}
//echo $buf;
socket_close($msgsock);
} while (true);
socket_close($ sock);
?>
client.php
Copy code
The code is as follows:
TCP/IP Connection h2>n";
/*
+---------------- ---------------
* @socket connection entire process
+---------------------- ---------
* @socket_create
* @socket_connect
* @socket_write
* @socket_read
* @socket_close
+------- --------------------------
*/
$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 "Attempting to connect to '$ip' port '$port'...n";
$result = socket_connect($socket, $ip, $port);
if ($result < 0) {
echo "socket_connect() failed.nReason: ($result) " . socket_strerror($result) . "n";
}else {
echo "Connect OKn";
}
$in = "Horn";
$in .= "first bloodrn";
$out = '';
if(!socket_write($socket, $in, strlen($in) )) {
echo "socket_write() failed: reason: " . socket_strerror($socket) . "n";
}else {
echo "Information sent to server successfully! n";
echo "The content sent is:$in
";
}
while($out = socket_read($ socket, 8192)) {
echo "Received server response information successfully! n";
echo "The accepted content is:",$out;
}
echo "Close SOCKET...n";
socket_close($socket);
echo "Close OKn";
?>
Listening
# /usr/local/php/bin/php /usr/local/apache2/htdocs/server.php
Request
# /usr/local/php/bin/php /usr/local/apache2/htdocs/client.php
#cd ./ext/sockets/
# /usr/local/php/bin/phpize
# ./configure --enable-sockets --with-php-config=/usr/local/php/bin/php-config
# make
# make install
php .ini modify configuration
Add
extension=sockets.so
Restart apache
# /usr/local/apache2/bin/apachectl restart
===================
Configuration under Windows
Modify php.ini
extension =php_sockets.dll
Restart apache
Cmd and start 2 places
php installation directory
C:/php/php.exe F:web/server.php
C: /php/php.exe F:web/client.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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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

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.

In this chapter, we are going to learn the following topics related to routing ?

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

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

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