PHP Use Socket Programming Example _ practical Tips

Source: Internet
Author: User

2 PHP Test files
server.php

Copy Code code as follows:

<?php
Phpinfo ();
Ensure that the client is not timed out when connected
Set_time_limit (0);

$ip = ' 127.0.0.1 ';
$port = 1935;

/*
+-------------------------------
* @socket the entire process of communication
+-------------------------------
* @socket_create
* @socket_bind
* @socket_listen
* @socket_accept
* @socket_read
* @socket_write
* @socket_close
+--------------------------------
*/

/*----------------The following actions are the-------------------in the manual * *
if ($sock = Socket_create (af_inet,sock_stream,sol_tcp)) < 0) {
The reason for the echo "socket_create () failure is:". Socket_strerror ($sock). \ n ";
}

if ($ret = Socket_bind ($sock, $ip, $port)) < 0) {
The reason for the echo "Socket_bind () failure is:". Socket_strerror ($ret). \ n ";
}

if ($ret = Socket_listen ($sock, 4)) < 0) {
The reason for the echo "Socket_listen () failure is:". Socket_strerror ($ret). \ n ";
}

$count = 0;

do {
if ($msgsock = socket_accept ($sock)) < 0) {
echo "Socket_accept () Failed:reason:". Socket_strerror ($msgsock). "\ n";
Break
} else {

Send to Client
$msg = "Test success!" \ n ";
Socket_write ($msgsock, $msg, strlen ($msg));

echo "Test succeeded AH \ n";
$buf = Socket_read ($msgsock, 8192);


$talkback = "received information: $BUF \ n";
Echo $talkback;

if (+ + $count >= 5) {
Break
};


}
Echo $buf;
Socket_close ($msgsock);

} while (true);

Socket_close ($sock);
?>


client.php

Copy Code code as follows:

<?php
Error_reporting (E_all);
Set_time_limit (0);
echo "

$port = 1935;
$ip = "127.0.0.1";

/*
+-------------------------------
* @socket connect the whole 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 "Attempts to connect the ' $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 ok\n";
}

$in = "ho\r\n";
$in. = "blood\r\n";
$out = ';

if (!socket_write ($socket, $in, strlen ($in)) {
echo "Socket_write () Failed:reason:". Socket_strerror ($socket). "\ n";
}else {
echo "Sent to server information successfully!" \ n ";
echo "Sends the content: <font color= ' red ' > $in </font> <br>";
}

while ($out = Socket_read ($socket, 8192)) {
echo "The receiving server returned the information successfully!" \ n ";
echo "accepted the content as:", $out;
}

echo "Close socket...\n";
Socket_close ($socket);
echo "Close ok\n";
?>

Listen

Copy Code code as follows:

#/usr/local/php/bin/php/usr/local/apache2/htdocs/server.php


Request

Copy Code code as follows:

#/usr/local/php/bin/php/usr/local/apache2/htdocs/client.php


The default PHP is not open socket

Copy Code code as follows:

#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
Increase
Copy Code code as follows:

Extension=sockets.so


Restart Apache
Copy Code code as follows:

#/usr/local/apache2/bin/apachectl Restart


Configuration under Windows
Modify PHP.ini
Copy Code code as follows:

Extension=php_sockets.dll

Restart Apache
CMD down from 2
installation directory for PHP

Copy Code code as follows:

C:/php/php.exe f:web/server.php
C:/php/php.exe f:web/client.php

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.