- today, finally, the test succeeded. Socket communication in PHP, first look at the schematic
- Here you can see clearly where the socket is in the network model, as well as the image representation of the port.
Our programming is in this order, please see the figure below
- so we can write the program, first look at the server side saved as serversocket.php
[PHP]View Plain copy print?
- <span style="Font-size:14px;color: #996633;" ><?php
- set_time_limit (0);
- $host="localhost";
- $port= 1000;
-
- //Create a connection
- $socket=socket_create (af_inet,sock_stream,sol_tcp)or die ("Cannot create socket\n");
- //bind socket to port
- $result=socket_bind ($socket,$host,$port ) or die("cannot bind port to socket\n");
- //start listening on this port
- $result=socket_listen ($socket, 4) or die("could not set up socket listen\n");
- //Accept connection, another socket to handle communication
- $msgsock=socket_accept ($socket) or die( "cannot accept incoming connection\n");
- if($msgsock) {
- echo date("y-m-d h:i:s D a");
- }
- //Read the message sent by the client .
- $input=socket_read ($msgsock, 1024x768) or die( "Cannot read input\n");
- $input=trim ($input);
- $output=strrev($input). "The order is reversed. \ n" ;
- //Docking The received information for processing and then returning to the client
- socket_write ($msgsock,$output,strlen($output ) or die ("Cannot write");
- //Close socket connection
- socket_close ($msgsock);
- socket_close ($socket);
-
- ?></span>
- then look at the client's code saved as client.php
[PHP]View Plain copy print?
- <span style="Font-size:14px;color: #996633;" ><?php
- set_time_limit (0);
- $host="localhost";
- $port= 1000;
-
- //Create a socket
- $socket=socket_create (af_inet,sock_stream,sol_tcp)or die ("Cannot create socket\n");
- $conn=socket_connect ($socket,$host,$port ) or die("Cannot connect server\n");
- if($conn) {echo "Client connect ok!" ;}
- socket_write ($socket,"Hello world!" ) or die("Cannot write data\n");
- $buffer=socket_read ($socket, 1024,php_normal_read);
- if($buffer) {
- echo "response was:". $buffer . "\ n" ;
- }
- socket_close ($socket);
- ?></span>
Note: 1 When testing the socket must determine whether you open the socket extension library, if not please go to php.ini to find Extension_dir=php_sockets.dll, the The front semicolon is removed
2 test, enter http://localhost/serverSocket.php and http://localhost/client.php in the browser, now the problem is coming, in the end first shipped OK, because the client responds, so let's run the server and run the client so you can see the results.
Socket Function Reference
socket_accept () accepts a socket connection
Socket_bind () binds the socket to an IP address and port
Socket_clear_error () clears the socket error or the last error code
Socket_close () Close a socket resource
Socket_connect () Start a socket connection
Socket_create_listen () Open a socket listener on the specified port
Socket_create_pair () produces a pair of undifferentiated sockets into an array
Socket_create () produces a socket equivalent to a data structure that produces a socket
socket_get_option () Get socket options
Socket_getpeername () Gets the IP address of a remote similar host
Socket_getsockname () Gets the IP address of the local socket
Socket_iovec_add () add a new vector to a scatter/aggregate array
Socket_iovec_alloc () This function creates a IOVEC data structure that can send the received read and write
socket_iovec_delete () Delete an assigned Iovec
Socket_iovec_fetch () returns the data for the specified Iovec resource
Socket_iovec_free () releasing a Iovec resource
Socket_iovec_set () sets the new data value of the Iovec
Socket_last_error () gets the last error code for the current socket
Socket_listen () listens for all connections by the specified socket
Socket_read () reads the specified length of data
SOCKET_READV () reads data from a scatter/aggregate array
socket_recv () end data from socket to cache
Socket_recvfrom () accepts data from the specified socket, if not specified, the default current socket
socket_recvmsg () receive messages from Iovec
Socket_select () multi-channel selection
socket_send () This function sends the data to the connected socket
socket_sendmsg () Send message to socket
socket_sendto () sends a message to the socket at the specified address
Socket_set_block () set as block mode in socket
Socket_set_nonblock () set to non-block mode in socket
socket_set_option () Set socket options
Socket_shutdown () This function allows you to close a read, write, or specified socket
Socket_strerror () returns a detailed error for the specified error number
Socket_write () write data to the socket cache
Socket_writev () write data to a scatter/aggregate array
PHP Socket Programming Communication