I am writing and reading data to and from a socket with PHP. My application is meant to pass a benchmark of handling 1000 requests at a time without failure, however when I am testing with Apache JMeter I am getting between 1 - 3% failure. When I check my log, I see the failure attributed to the error below:
[2019-08-07 08:27:53] production.DEBUG: SOCKET EXCEPTION OCCURED WHILE ENCRYPTING ["socket_write(): unable to write to socket [10057]: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied.
"]
[2019-08-07 08:27:53] production.DEBUG: SOCKET EXCEPTION OCCURED WHILE CONNECTING TO SOCKET ["socket_connect(): unable to connect [10049]: The requested address is not valid in its context.
"]
Here's my socket connection code:
/**
* Created by PhpStorm.
* User: Michel
* Date: 08/01/19
* Time: 8:23 AM
*/
namespace App\Repository;
use Illuminate\Support\Facades\Log;
class SocketHandler
{
private static $response = ['error' => false, 'message' => ''];
private static $socket;
private static function createSocket()
{
$address = env('SOCKET_IP');
$service_port = env('SOCKET_PORT');
/* Create a TCP/IP socket. */
try{
static::$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
// an error occur while creating socket, return error response.
if (static::$socket === false) {
static::$response['error'] = true;
static::$response['message'] =
socket_strerror(socket_last_error());
return false;
}
socket_set_nonblock($sock);
}catch(\Exception $e){
Log::debug("SOCKET EXCEPTION OCCURED WHILE CREATING SOCKET CONNECTION", [$e->getMessage()]);
}
// create a socket connection.
try{
$result = socket_connect(static::$socket, $address, $service_port);
// an error occur while connecting to socket. return error response.
if ($result === false) {
static::$response['error'] = true;
static::$response['message'] = ($result) .' '. socket_strerror(socket_last_error(static::$socket));
return false;
}
}catch(\Exception $e){
Log::debug("SOCKET EXCEPTION OCCURED WHILE CONNECTING TO SOCKET", [$e->getMessage()]);
}
}
public static function encrypt($message)
{
try{
if (static::createSocket() === false ) {
return static::$response;
}
// attach encryption command to the message that is to be encrypted.
$message = "ENC$message";
// write the message to the socket connection
socket_write(static::$socket, $message, strlen($message));
// read response from socket connection
while ($out = socket_read(static::$socket, 2048)) {
static::$response['error'] = false;
static::$response['message'] = $out;
}
return static::cipher(static::$response['message']);
}catch(\Exception $e){
Log::debug("SOCKET EXCEPTION OCCURED WHILE ENCRYPTING", [$e->getMessage()]);
}
}
public static function decrypt($message)
{
try{
if (static::createSocket() === false ) {
return static::$response;
}
// attach decryption command to the message that is to be decrypted.
$pass = env('SSM_PASS');
$message = "DEC$pass#$message";
// write the message to the socket connection
socket_write(static::$socket, $message, strlen($message));
// read response from socket connection
while ($out = socket_read(static::$socket, 2048)) {
static::$response['error'] = false;
static::$response['message'] = $out;
}
return static::decipher(static::$response['message']);
}catch(\Exception $e){
Log::debug("SOCKET EXCEPTION OCCURED WHILE DECRYPTING", [$e->getMessage()]);
}
}
public static function cipher($message)
{
// attach cipher command to the message that is to be ciphered.
$message = "ENC$message";
// write the message to the socket connection
socket_write(static::$socket, $message, strlen($message));
// read response from socket connection
while ($out = socket_read(static::$socket, 2048)) {
static::$response['error'] = false;
static::$response['message'] = $out;
}
return static::$response;
}
public static function decipher($message)
{
// attach decipher command to the message that is to be deciphered.
$message = "DEC$message";
// write the message to the socket connection
socket_write(static::$socket, $message, strlen($message));
// read response from socket connection
while ($out = socket_read(static::$socket, 2048)) {
static::$response['error'] = false;
static::$response['message'] = $out;
}
return static::$response;
}
}
Could there be a better way to handle multiple connections to socket whenever I have such number of requests to the system at once?