A
A
Andrey2020-02-10 14:43:35
WordPress
Andrey, 2020-02-10 14:43:35

How to catch an exception from the fwrite command?

There is a Yii2 console application.

ActionIndex
public function actionIndex()
    {
        $ip = 'tcp://192.168.5.162';
        $port = 10001;
        $storage = 'Автоприем';
        $this->fp = $this->init_rfid($ip,$port, $storage);
        while($this->fp <> 0){
            $this->read_rfid($storage);
            usleep(1500000);
        }
    }


In the init_rfid method, a request is sent to the device, initializing and blocking the channel to communicate with it.
init_rfid method
$fp = @fsockopen($ip,$port,$errno, $errstr, 10);
if (!$fp) {
      print_r("ERROR: $errno - $errstr\n");
      $fp = 0;
} else{
...
}

Every 1.5 seconds I read the state from it.
But after some time (a day, a week), the connection is reset on the device side and the fwrite command in the read_rfid method flies away with an error
Error: fwrite(): send of 180 bytes failed with errno=10054

I don't want to initialize every 1.5 seconds, because it needs to send 2 requests to the device.
How do I control the connection or handle the fwrite exception?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Ruslan Ruslanov, 2019-05-30
@dasauser

run a bulk search in ide and find the button you need.

M
Mikhail Kobzarev, 2019-05-31
@mihdan

You can use the Loco Translate plugin , for example.

L
Lazy @BojackHorseman PHP, 2020-02-10
Tag

this function does not throw exceptions.

How to catch

Throwable or crutch in early versions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question