Sockets are used for interprocess communication. Interprocess communication is typically based on a client-server model. At this point, the client-server is an application that can interact with each other. The interaction between the client and the server requires a connection. Socket programming is responsible for establishing an interactive connection between applications.
Using PHP to create a simple client-server, develop a client to send a string message to the server, the service side will reverse the same information back to the client.
Server-Sidethe list content setting host and port number can be any positive integer between 1024-65535。
$host"127.0.0.1";$port=5353;set_time_limit(0);//设置程序的执行时间 当为0的时候永久执行直到程序结束,如果为大于零的数字,则不管程序是否执行完成,到了设定的秒数,程序结束。
Create socket
$socket = socket_create(AF_INET,SOCK_STREAM,0ordie("could not create socket\n");
the socket resource created is bound to the IP address and port number.
$result = socket_bind($socket$host$portordie("Could not bind to socket\n");
Start Socket Monitoring
After binding to IP and ports, the server begins to wait for the client to connect. It waits until the connection is not connected.
$result = socket_listen($socket,3ordie("Could not set up socket listener\n");
Accept Connection
This function will accept incoming connection requests from the built socket. After accepting the connection from the client socket, the function returns another socket resource, which is actually responsible for communicating with the corresponding client socket. The "$spawn" here is the socket resource responsible for communicating with the client socket.
$spawn = socket_accept($socketordie("Could not accept incoming connection\n");
reading messages from the client socket
$input = socket_read($spawn,1024ordie("Could not read input\n");
Reverse Message
$output = strrev($input)."\n";
send a message to the client socket
socket_write($spawn,$output,strlen(($outputordie("Could not write output\n");
Close Socket
socket_close($spawn);socket_close($socket);
Clientsetting up hosts and Ports
Note: The ports and hosts here should be the same as those defined in the server.
$host"127.0.0.1";$port=5353;set_time_limit(0);
the information to be sent
$message"xiongchao";echo"Message To server :".$message."\n";
Create socket
$socket = socket_create(AF_INET,SOCK_STREAM,0ordie("Could not create socket\n");
Connect to the service side
At this point, unlike the server, the client socket is not bound to the port and host. Instead, it connects to the server socket and waits for the connection from the client socket to be accepted.
This step establishes the client socket connection to the server socket.
$result = socket_connect($socket,$host,$portordie("Could not connect to server\n");
write to the service-side socket
In this step, the client socket data is sent to the server socket.
socket_write($socket,$message,strlen($messageordie("Could not send data to server\n");
read responses from the server
$result = socket_read($socket,1024ordie("Could not read server response\n");echo"Reply from server:".$result;
Close Socket
socket_close($socket);
Running CommunicationsRun server-side
Then check to see if the port is listening:
You can see that the port is listening.
running the client
When the server side is successfully running and the port is being monitored, the client is run at this time
The message was successfully sent to the server side and received the confidence that the server side returned.
Socket Correlation Function
- 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 indistinguishable 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 already 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
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
PHP Socket Communication Programming