PHPSocket communication programming Socket is used for inter-process communication. Usually, inter-process communication is based on the client? Server model. At this time, the client? The server is an application that can interact with each other. The interaction between the client and the server needs to be connected. Socket programming is responsible for establishing interactive connections between applications.
How can I create a simple client using PHP? A client is developed to send a string message to the server. the server reverts the same information and returns it to the client.
Server The host and port numbers in the list can be any positive integers between 1024 and 65535..
$ Host = "127.0.0.1"; $ port = 5353; set_time_limit (0); // set the execution time of the program to be permanently executed until the program ends, if it is a number greater than zero, the program ends no matter whether the program is executed successfully or not.
Create socket
$socket = socket_create(AF_INET,SOCK_STREAM,0) or die("could not create socket\n");The created socket resource is bound to the IP address and port number.
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");Start socket listening
After being bound to an IP address and port, the server waits for the connection from the client. Wait until the connection is established.
$result = socket_listen($socket,3) or die("Could not set up socket listener\n");Accept connection
This function will accept the connection request passed in by the socket. After accepting the connection from the client socket, this function returns another socket resource, which is actually responsible for communicating with the corresponding client socket. $ Spawn is the socket resource responsible for communicating with the client socket.
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");Read messages from client socket
$input = socket_read($spawn,1024) or die("Could not read input\n");Reverse message
$output = strrev($input)."\n";
Send a message to the client socket
socket_write($spawn,$output,strlen(($output))) or die("Could not write output\n");Disable socket
socket_close($spawn);socket_close($socket);
Client Set host and port
Note: the ports and hosts here should be the same as the definitions in the server.
$host = "127.0.0.1";$port=5353;set_time_limit(0);
Information to be sent
$message = "xiongchao";echo "Message To server :".$message."\n";
Create socket
$socket = socket_create(AF_INET,SOCK_STREAM,0) or die("Could not create socket\n");Connect to the server
In this case, the client socket is not bound to the port and host. Instead, it connects to the server socket and waits to accept the connection from the client socket.
This step establishes a connection from the client socket to the server socket.
$result = socket_connect($socket,$host,$port) or die("Could not connect to server\n");Write to server socket
In this step, the client socket data is sent to the server socket.
socket_write($socket,$message,strlen($message)) or die("Could not send data to server\n");Read responses from the server
$result = socket_read($socket,1024) or die("Could not read server response\n");echo "Reply from server:".$result;Disable socket
socket_close($socket);
Operation communication Running server
Then, check whether the port is listened on:
You can see that the port has been listened on
Run the client
When the server runs successfully and the port is listened to, the client is running
The information is successfully sent to the server, and the confidence that the server returns
Socket-related functions
Socket_accept () accepts a Socket connection
Socket_bind () binds the socket to an IP address and port.
Socket_clear_error () clears socket errors or the final error code
Socket_close () closes a socket resource
Socket_connect () starts a socket connection
Socket_create_listen () opens a socket listener on the specified port.
Socket_create_pair () generates a pair of identical sockets into an array.
Socket_create () generates a socket, which is equivalent to the data structure of a socket.
Socket_get_option () obtain the socket option
Socket_getpeername () obtains the IP address of a remote host similar to that of a remote host.
Socket_getsockname () gets the IP address of the local socket
Socket_iovec_add () add a new vector to a scattered/aggregated array
Socket_iovec_alloc () this function creates an iovec data structure that can send and receive read/write data.
Socket_iovec_delete () deletes an allocated iovec
Socket_iovec_fetch () returns the data of the specified iovec resource.
Socket_iovec_free () releases an iovec resource.
Socket_iovec_set () sets the new value of iovec data
Socket_last_error () obtains the final error code of the current socket.
Socket_listen () listens to all connections from the specified socket
Socket_read () reads data of the specified length
Socket_readv () reads data from scattered/aggregate arrays
Socket_recv () ends data from the socket to the cache
Socket_recvfrom () accepts data from the specified socket. If no value is specified, the current socket is used by default.
Socket_recvmsg () receives messages from iovec
Socket_select () multi-path selection
Socket_send () this function sends data to the connected socket
Socket_sendmsg () sends a message to the socket
Socket_sendto () sends a message to the socket of the specified address
Socket_set_block () is set to block mode in socket
Set the socket in socket_set_nonblock () to non-block mode.
Socket_set_option () sets socket options
The socket_shutdown () function allows you to close the read, write, or specified socket
Socket_strerror () returns a detailed error of the specified error number
Socket_write () writes data to the socket cache
Socket_writev () writes data to a distributed/aggregate array
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.