This article provides an example of a socket-related function in PHP, if a socket is created to accept an operation such as a socket initialization connection. */
This article provides an example of a socket-related function in a PHP tutorial, if a socket is created to accept an operation such as a socket initialization connection. */
$ip = "127.0.0.1"; Define IP Address
$port = 1000; Defining ports
$socket =socket_create (AF_INET,SOCK_STREAM,SOL_TCP); Create socket
$bind =socket_bind ($socket, $ip, $prot); Bind a socket
$listen =socket_listen ($socket); Monitor socket
$msg =socket_accept ($socket); Accept a socket connection
$welcome = "Welcome to PHP service/n"; Defining strings
Socket_write ($msg, $welcome, strlen ($welcome)); Write a socket
Socket_close ($socket); Close socket
/*
*/
$ip = "127.0.0.1"; Define IP Address
$port = 1000; Defining ports
$socket =socket_create (AF_INET,SOCK_STREAM,SOL_TCP); Create socket
Socket_clear_error ($socket); Empty the socket error message
Socket_close ($socket); Close socket
/*
*/
$sockets =array (); Define an array
/* Create a pair of sockets and store them in the array */
if (!socket_create_pair (af_unix,sock_stream,0, $sockets))//If the socket was not created successfully
{
Echo Socket_strerror (Socket_last_error ()); Output error message
}
/* Send and receive information using the created socket */
if (!socket_write ($sockets [0], "abcdef123n", strlen ("abcdef123n"))//If execution of 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]); Close Socket1
Socket_close ($sockets [1]); Close Socket2
/*
*/
$ip = "127.0.0.1"; Define IP Address
$port = 1000; Defining ports
$socket =socket_create (AF_INET,SOCK_STREAM,SOL_TCP); Create socket
Socket_set_nonblock ($socket); Set not locked state
Socket_connect ($socket, $ip, $port); Initializing a connection
Socket_set_block ($sock); Lock socket
Switch (Socket_select ($r =array ($socket), $w =array ($socket), $f =array ($socket), 5))
{
Case 2:
echo "Connection is denied! n ";
Break
Case 1:
echo "Successfully connected n";
Break
Case 0:
echo "Connection timeout n";
Break
}
/*
*/
/* The following code defines a socket listener class */
Class SocketListener extends socket
{
function __construct ($port =null)//define __construct method
{
$this->stream= @socket_create_listen ($port $port:p ort); Open Port Accept Connection
}
function isactive ()//define IsActive method
{
if ($this->stream)//If there is a connection
{
return true; Return truth
}
else//No connection
{
return false; return False value
}
}
}
http://www.bkjia.com/PHPjc/444819.html www.bkjia.com true http://www.bkjia.com/PHPjc/444819.html techarticle This article provides an example of a socket-related function in PHP, if a socket is created to accept an operation such as a socket initialization connection. * * This article provides a tutorial in PHP sock ...