PHP socket in the use of detailed _php tips

Source: Internet
Author: User

This article details the usage of socket in PHP and share it for everyone's reference. The specific usage is as follows:

First, open the socket

Phpinfo () to see if the socket extension is turned on or open in php.ini.

Second, the server-side code of the wording

Copy Code code as follows:
<?php
Error_reporting (E_all);
Set_time_limit (0);
Ob_implicit_flush ();

$address = ' 127.0.0.1 ';
$port = 10005;
Create Port
if (($sock = Socket_create (Af_inet, Sock_stream, sol_tcp)) = = False) {
echo "Socket_create () Failed:reason:". Socket_strerror (Socket_last_error ()). "\ n";
}

Binding
if (Socket_bind ($sock, $address, $port) = = False) {
echo "Socket_bind () Failed:reason:". Socket_strerror (Socket_last_error ($sock)). "\ n";
}

//Listener
if (Socket_listen ($sock, 5) = = False) {
 echo "Socket_bind () Failed:reason:". Socket_strerror (Socket_last_error ($sock)). "\ n";
}

do {
Get a link
if (($msgsock = socket_accept ($sock)) = = False) {
echo "Socket_accepty () Failed:reason:". Socket_strerror (Socket_last_error ($sock)). " \ n ";
Break
}
Welcome sent to Client
$msg = "<font color= ' red ' >server send:welcome</font><br/>";
Socket_write ($msgsock, $msg, strlen ($msg));
Echo ' read client message\n ';
$buf = Socket_read ($msgsock, 8192);
$talkback = "received message: $BUF \ n";
Echo $talkback;
if (false = = Socket_write ($msgsock, $talkback, strlen ($talkback))) {
echo "Socket_write () failed reason:". Socket_strerror (Socket_last_error ($sock)). " \ n ";
} else {
Echo ' send success ';
}
Socket_close ($msgsock);
} while (true);
Close socket
Socket_close ($sock);
?>

Server side needs to be executed in CLI mode, it is possible that the php.ini file load is different in CLI mode

You can output as follows:

In this case, there is a Tem.text file in the Zhoxh directory. View Configuration File (php.ini) Path => C:\WINDOWS. Not my php.ini file, which indicates that the call to the php.ini file was wrong. This time we want to specify the php.ini file command as follows

Note that my PHP can be configured with the environment variables when it is executed directly.

Third, the client

Copy Code code as follows:
<?php
Error_reporting (E_all);
echo "&LT;H2&GT;TCP/IP connection $service _port = 10005;
$address = ' 127.0.0.1 ';

$socket = Socket_create (Af_inet, Sock_stream, sol_tcp);
if ($socket = = False) {
echo "Socket_create () Failed:reason:". Socket_strerror (Socket_last_error ()). "\ n";
} else {
echo "OK. \ n ";
}

echo "Attempting to connect to" $address ' on port ' $service _port ' ... ";
$result = Socket_connect ($socket, $address, $service _port);
if ($result = = False) {
echo "Socket_connect () Failed.\nreason: ($result)". Socket_strerror (Socket_last_error ($socket)). "\ n";
} else {
echo "OK \ n";
}
$in = "head/http/1.1\r\n";
$in. = "host:localhost \ r \ n";
$in. = "connection:close\r\n\r\n";
$out = "";
echo "Sending HTTP Head request ...";
Socket_write ($socket, $in, strlen ($in));
echo "ok\n";

echo "Reading response:\n\n";
while ($out = Socket_read ($socket, 8192)) {
Echo $out;
}
echo "closeing socket ...";
Socket_close ($socket);
echo "OK. \ n";



The results of the implementation are as follows:
Server

Client

I hope this article will help you with your PHP program design.

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.