PHP Socket function reference _ PHP Tutorial

Source: Internet
Author: User
PHP Socket function reference. These Socket functions send messages directly with the Internet Protocol. Compared with the fopensock stream, they operate at a relatively low level. Generally, they all block C functions.

These Socket functions send messages directly with the Internet Protocol. Compared with the fopensock stream, they operate at a relatively low level. Generally, they encapsulate C functions with similar names. If you have experience in using C for socket programming, you will be very skilled in using these functions. We will not discuss socket programming in detail here.
Using these functions can solve the problems that upper-level functions cannot solve. Using these functions can implement functions similar to fopen. you may have many methods to implement socket functions, such as the Internet daemon process implemented by using Command-line Interface in PHP.
Resource socket_accept (resource socket)
On your script server, use socket_accept to accept an incoming connection. You must first generate a socket, bind it to a name, and set it to listen to a port. In block mode, socket_accept will generate a unique accepted connection. In non-block mode, if no connection is established, false is returned. In addition, when you have a new socket resource, you can perform read and write operations.
Next we will demonstrate a simple echo server. It runs under CLI and waits for the client connection on port 12345.
Socket_accept
Set_time_limit (0 );
// Create the socket
If ($ socket = socket_create (AF_INET, SOCK_STREAM, 0) <0 ){
Print ("Couldnt create socket:". socket_strerror (socket_last_error ())."");
}
// Bind it to the given address and port
If ($ error = socket_bind ($ socket, gethostbyname ($ _ SERVER [HOSTNAME]), 12345) <0 ){
Print ("Couldnt bind socket:". socket_strerror (socket_last_error ())."");
}
If ($ error = socket_listen ($ socket, 5) <0 ){
Print ("Couldnt list on socket :".
Socket_strerror (socket_last_error ())."");
}
While (TRUE ){
// Wait for connection
If ($ accept = socket_accept ($ socket) <0 ){
Print ("Error while reading:". socket_strerror ($ message )."");
Break;
}
// Send welcome message
Socket_write ($ accept, "Connection accepted ");
Print (date (Y-m-d H: I: s). "STATUS: Connection accepted ");
Ob_flush ();
While (TRUE ){
// Read line from client
If (FALSE = ($ line = socket_read ($ accept, 1024 ))){
Print ("Couldnt read from socket :".
Socket_strerror (socket_last_error ())."");
Break 2;
}
If (! @ Socket_write ($ accept, "ECHO: $ line ")){
Print (date (Y-m-d H: I: s). "STATUS: Connection interrupted ");
Break;
}
Print (date (Y-m-d H: I: s). "READ: $ line ");
Ob_flush ();
}
Socket_close ($ accept );
}
?>

Bool socket_bind (resource socket, string address, integer port)
This socket_bind () binds a socket resource to an address. This socket must be a resource returned by the socket_create () function. This address must be an IP address or a path for saving Unix socket. If it is a socket running on the Internet, you must also provide a port.
Socket_clear_error (resource socket)
This function can clear socket errors. If no parameter is specified, all socket errors will be cleared.
Socket_close (resource socket)
The socket_close function closes a socket and clears the memory resources occupied by the socket.
Boolean socket_connect (resource socket, string address, integer port)
This function creates a connection from a client to a port or socket. You must provide a socket generated by socket_create. This address parameter must be in the socket path or an IP address. If it is the latter, it must also be connected to the port number of a number.
The following example demonstrates how to connect to the game server using UDP and then obtain information.
Socket_connect
// Create UDP socket
If ($ socket = socket_create (AF_INET, SOCK_DGRAM, SOL_UDP) <0 ){
Print ("Couldnt create socket :".
Socket_strerror (socket_last_error ())."");
}
// Timeout after 5 seconds
Socket_set_option ($ socket, SOL_SOCKET,
SO_RCVTIMEO, array (sec => 5, usec => 0 ));
// Connect to the RtCW master server
If (! Socket_connect ($ socket, wolfmaster.idsoftware.com, 27950 )){
Print ("Couldnt connect :".
Socket_strerror (socket_last_error ())."");
}
// Send request for servers
Socket_write ($ socket, "xFFxFFxFFxFFgetserversx00 ");
// Get servers
$ Server = array ();
While (FALSE! ==( $ Line = @ socket_read ($ socket, 4096 ))){
// Parse data
For ($ I = 22; ($ I + 5) <strlen ($ line); $ I + = 7 ){
$ Ip = ord (substr ($ line, $ I + 1, 1 ))...
Ord (substr ($ line, $ I + 2, 1 ))...
Ord (substr ($ line, $ I + 3, 1 ))...
Ord (substr ($ line, $ I + 4, 1 ));
$ Port = (ord (substr ($ line, $ I + 5, 1) * 256) +
Ord (substr ($ line, $ I + 6, 1 ));
$ Server [] = array (ip => $ ip, port => $ port );
}
}
Print ("". count ($ server). "Servers ");
// Loop over servers, getting status
Foreach ($ server as $ s ){
& Nb

Bytes. Compared with the fopensock stream, they operate at a relatively low level. Generally, they all block C functions...

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.