Php Socket creates listeners and other instances

Source: Internet
Author: User
Tags socket error

This article provides an example of socket-related functions in php. If you create a Socket, you will be allowed to write a socket connection to initialize the connection. */

This article provides an example of socket-related functions in the php tutorial. If you create a socket, you will be allowed to write a socket connection to initialize the connection. */
$ 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.
}
}
}


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.