Socket Programming in PHP

Ahsan Habib
3 min readJan 20, 2020

--

What is socket programming?

Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while other socket reaches out to the other to form a connection. The server forms the listener socket while the client reaches out to the server.

State diagram for server and client model

State diagram for server and client model
Image From google search

Now are Develop a client to send a string message to server and server to return reverse of the same message to the client.

PHP SERVER

Step 1: Set variables such as host and port

$host = “127.0.0.1”; // your localhost IP 
$port = 5353; // Port number can be any positive integer between 1024 -65535.
set_time_limit(0); // No Timeout

Step 2: Create Socket

$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die(“Could not create socket\n”);

Step 3: Bind the socket to port and host

Here the created socket resource is bound to IP address and port number.

$result = socket_bind($socket, $host, $port) or die(“Could not bind to socket\n”);

Step 4: Start listening to the socket

After getting bound with IP and port server waits for the client to connect. Till then it keeps on waiting.

$result = socket_listen($socket, 3) or die(“Could not set up socket listener\n”);

Step 5: Accept incoming connection

This function accepts an incoming connection request on the created socket. After accepting the connection from the client socket, this function returns another socket resource that is actually responsible for communication with the corresponding client socket. Here “$spawn” is that socket resource which is responsible for communication with client socket.

$spawn = socket_accept($socket) or die(“Could not accept incoming connection\n”);

So far, we have prepared our server socket but the script doesn’t actually do anything. Keeping to our aforesaid aim, we will read a message from the client socket and then send back reverse of the received message to the client socket again.

Step 6: Read the message from the Client socket

$input = socket_read($spawn, 1024) or die(“Could not read input\n”);

Step 7: Reverse the message

$output = strrev($input) . “\n”;

Step 8: Send message to the client socket

socket_write($spawn, $output, strlen ($output)) or die(“Could not write output\n”);

Close the socket

socket_close($spawn);
socket_close($socket);
This completes with the server. Now we will learn to create PHP client.
PHP CLIENT

The first two steps are the same as in the server.

Step 1: Set variables such as “host” and “port”

$host = “127.0.0.1”;
$port = 5353;
// No Timeout
set_time_limit(0);
Note: Here the port and host should be same as defined in server.

Step 2: Create Socket

$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die(“Could not create socket\n”);

Step 3: Connect to the server

$result = socket_connect($socket, $host, $port) or die(“Could not connect toserver\n”);

Here unlike server, client socket is not bound with port and host. Instead, it connects to server socket, waiting to accept the connection from client socket. Connection of client socket to server socket is established in this step.

Step 4: Write to server socket

socket_write($socket, $message, strlen($message)) or die(“Could not send data to server\n”);

In this step, client socket data is sent to the server socket.

Step 5: Read the response from the server

$result = socket_read ($socket, 1024) or die(“Could not read server response\n”);
echo “Reply From Server :”.$result;

Step 6: Close the socket

socket_close($socket);Complete Code

SERVER (server.php)

CLIENT (client.php)

After creating the above files (server.php and client.php), do as follows:

Copy these files in www directory (in case of WAMP), located at C:\wamp.
Open your web browser and type localhost in the address bar.
Browse server.php first followed by client.php.

--

--

Ahsan Habib
Ahsan Habib

Responses (1)