dongwuge6201 2019-08-07 08:30
浏览 79

在一次发送多个请求时丢失与套接字的连接 - PHP

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?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python
    • ¥15 要给毕业设计添加扫码登录的功能!!有偿
    • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
    • ¥15 微信公众号自制会员卡没有收款渠道啊
    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘