PHP Socket creation Monitoring and other instances _php tutorial

Source: Internet
Author: User
Tags socket error
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 ...

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