How to implement socket creation and monitoring in PHP _php tips

Source: Internet
Author: User

This article is an example of PHP socket creation and monitoring implementation method. Share to everyone for your reference. Specifically as follows:

This provides an example of a socket-related function in PHP, such as creating a socket, accepting a socket connection, writing a socket and initializing the connection, and so on, the specific example code is as follows:

Copy Code code as follows:
$ip = "127.0.0.1"; Define IP Address
$port = 1000; Defining ports
$socket =socket_create (AF_INET,SOCK_STREAM,SOL_TCP); Create a socket
$bind =socket_bind ($socket, $ip, $prot); Bind a socket
$listen =socket_listen ($socket); Listening for sockets
$msg =socket_accept ($socket); Accept a socket connection
$welcome = "Welcome to PHP service/n"; Define String
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 a socket
Socket_clear_error ($socket); Empty socket error message
Socket_close ($socket); Close socket

/*

*/
$sockets =array (); Define an array
/* Create a pair of sockets and store them in an array.
if (!socket_create_pair (af_unix,sock_stream,0, $sockets))//If the socket was not successfully created
{
Echo Socket_strerror (Socket_last_error ()); Output error message
}
/* Use the socket created to send and receive information * *
if (!socket_write ($sockets [0], "abcdef123n", strlen ("abcdef123n"))///If the Execute socket write fails
{
Cho Socket_strerror (Socket_last_error ()); Output error message
}
if (! $data =socket_read ($sockets [1],strlen ("abcdef123n"), Php_binary_read)//If read 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 a socket
Socket_set_nonblock ($socket); Set No lock status
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 denied! n ";
Break
Case 1:
echo "successfully connects 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)//Definition __construct method
{
$this->stream= @socket_create_listen ($port $port:p ort); Open Port Accept Connection
}
function isactive ()//Definition IsActive method
{
if ($this->stream)//If there is a connection
{
return true; Return truth value
}
else//No connection
{
return false; return False value
}
}
}

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.