Home >Backend Development >PHP Tutorial >php Socket creation and monitoring examples_PHP tutorial

php Socket creation and monitoring examples_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-20 11:09:13932browse

This article provides examples of socket-related functions in PHP, such as creating a Socket, accepting a socket connection, writing a Socket, initializing the connection, and other operations. */

This article provides examples of socket-related functions in the PHP tutorial. If you create a socket, accept a socket connection, write a socket, initialize the connection and other operations. */
$ip="127.0.0.1"; //Define ip address
$port=1000; //Define port
$socket=socket_create(af_inet,sock_stream,sol_tcp); //Create socket
$bind=socket_bind($socket,$ip,$prot); //Bind a socket
$listen=socket_listen($socket); //Listen to the socket
$msg=socket_accept($socket );                                                                                                                      Enter a socket
socket_close($socket);                                                                                                                     ip address
$port=1000; //Define port
$socket=socket_create(af_inet,sock_stream,sol_tcp); //Create socket

socket_clear_error($socket); //Clear the socket error message

socket_close($socket); ; Create a pair of sockets and store them in the array */

if(!socket_create_pair(af_unix,sock_stream,0,$sockets)) //If the socket is not successfully created

{

echo socket_strerror(socket_last_error() ); //Output error message

}
/*Use the created socket to send and receive information*/
if(!socket_write($sockets[0],"abcdef123n",strlen("abcdef123n"))) //If the socket write fails
{
cho socket_strerror(socket_last_error()); //Output error message

}

if(!$data=socket_read($sockets[1],strlen( "abcdef123n"),php_binary_read)) //If reading information fails
{

echo socket_strerror(socket_last_error()); //Output error message

}
var_dump($data); Output socket data information
/*Close socket*/
socket_close($sockets[0]); >/*
*/

$ip="127.0.0.1"; //Define ip address
$port=1000; //Define port
$socket=socket_create(af_inet, sock_stream,sol_tcp); //Create socket
socket_set_nonblock($socket); ); //Lock socket
switch(socket_select($r=array($socket),$w=array($socket),$f=array($socket),5))
{
case 2:
echo "Connection refused! n";
break;
case 1:
echo "Successful connection n";
break;
case 0:
echo "Connection timeout n";

break;

}

/*


*/

/*The following code defines a socket listening class*/
class socketlistener extends socket
{
function __construct($port=null) //Define __construct method
{
$ this->stream=@socket_create_listen($port?$port:port); //Open the port to accept connections
}
function isactive() //Define the isactive method
{
if($ this->stream) //If there is a connection
{
return true; //Return a true value
}
else return false;           // Return false value
}
}
}


http://www.bkjia.com/PHPjc/444819.html

truehttp: //www.bkjia.com/PHPjc/444819.htmlTechArticleThis article provides examples of socket-related functions in php. If you create a Socket, accept a socket connection and write a Socket initialization Connection and other operations. */ This article provides sock in the php tutorial...
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