$ Ip = "127.0.0.1"; // define an ip address
$ Port = 1000; // define the port
$ 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); // listen to socket
$ Msg = socket_accept ($ socket); // accept a socket connection
$ Welcome = "welcome to the php service/n"; // define a string
Socket_write ($ msg, $ welcome, strlen ($ welcome); // write a socket
Socket_close ($ socket); // Close the socket
/*
*/
$ Ip = "127.0.0.1"; // define an ip address
$ Port = 1000; // define the port
$ Socket = socket_create (af_inet, sock_stream, sol_tcp); // Create a socket
Socket_clear_error ($ socket); // clear the socket Error message
Socket_close ($ socket); // Close the socket
/*
*/
$ Sockets = array (); // defines an array
/* Create a socket and store it 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 the error message
}
/* Use the created socket to send and receive messages */
If (! Socket_write ($ sockets [0], "abcdef123n", strlen ("abcdef123n") // if the socket write fails to be executed
{
Cho socket_strerror (socket_last_error (); // output the error message
}
If (! $ Data = socket_read ($ sockets [1], strlen ("abcdef123n"), php_binary_read) // if the read information fails
{
Echo socket_strerror (socket_last_error (); // output the error message
}
Var_dump ($ data); // output socket data information
/* Disable socket */
Socket_close ($ sockets [0]); // close socket1
Socket_close ($ sockets [1]); // close socket2
/*
*/
$ Ip = "127.0.0.1"; // define an ip address
$ Port = 1000; // define the port
$ Socket = socket_create (af_inet, sock_stream, sol_tcp); // Create a socket
Socket_set_nonblock ($ socket); // you can specify the lock status.
Socket_connect ($ socket, $ ip, $ port); // initialize the connection
Socket_set_block ($ sock); // lock the socket
Switch (socket_select ($ r = array ($ socket), $ w = array ($ socket), $ f = array ($ socket), 5 ))
{
Case 2:
Echo "connection rejected! 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) // defines the _ construct method
{
$ This-> stream = @ socket_create_listen ($ port? $ Port: port); // open the port to accept the connection
}
Function isactive () // defines the isactive method.
{
If ($ this-> stream) // if there is a connection
{
Return true; // returns the true value.
}
Else // No Connection
{
Return false; // returns a false value.
}
}
}