PHP socket creation and monitoring implementation method, Phpsocket to create a listening _php tutorial

Source: Internet
Author: User
Tags socket error

PHP socket creation and monitoring implementation method, Phpsocket create monitoring


This paper introduces how to implement socket creation and monitoring in PHP. Share to everyone for your reference. Specific as follows:

Here is an example of a socket-related function in PHP, such as creating a socket, accepting a socket connection, writing a socket and initializing a connection, and so on, with the following example code:
Copy CodeThe code is as follows: $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
}
}
}

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/937726.html www.bkjia.com true http://www.bkjia.com/PHPjc/937726.html techarticle in PHP, the socket creation and monitoring implementation method, Phpsocket create a listener This article describes the socket creation and monitoring in PHP implementation method. Share to everyone for your reference. As follows: Here ...

  • 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.