-2

I am writing a socket client that connects to a server and does some interfacing, gets a result, and closes. However, sometime that server may be down or unavailable, and I want to show my users a nice screen showing that the system is currently down.

How would I handle that instead of having it spew all these warnings? I assume try/catch will not work since it's not actually throwing an exception.

I read dozens of answers, but they all involved a global warning handler, which I can't use for 2 reasons: I wanna know what went wrong, and I don't wanna call it a gazillion times (usually if something goes wrong, a lot of things goes wrong).

Warning: socket_connect(): unable to connect [10061]: No connection could be made because the target machine actively refused it. in C:\xampp\htdocs\TCPServices\Sockets.php on line 7

Warning: socket_write() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\TCPServices\Sockets.php on line 18

Warning: socket_read() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\TCPServices\Sockets.php on line 24

Warning: socket_close() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\TCPServices\Sockets.php on line 31

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Suppress them using `@` or make sure they don't occur. – Angel Politis Apr 03 '18 at 23:36
  • did you try @before function – Jehad Ahmad Jaghoub Apr 03 '18 at 23:36
  • Could someone please link me to a resource describing the @ function? If you supress them, can you check if the connection was made afterwards? – Takeru Nakajima Apr 03 '18 at 23:38
  • error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING); this will report to you only fatal error – Jehad Ahmad Jaghoub Apr 03 '18 at 23:39
  • Have you read the documentation of [`socket_connect()`](http://php.net/manual/en/function.socket-connect.php)? It returns `FALSE` on error. Check its return value and avoid doing any operation if it fails. This removes the very source of the warnings you listed. – axiac Apr 03 '18 at 23:39
  • @JehadAhmadJaghoub Many errors reported by PHP as warnings or notices are serious errors (sometimes fatal errors) for the application. This question is only one example of such an error. Suppressing the reporting of errors does not solve them. The errors and warnings must not be suppress, they must not be displayed on screen but logged into the log file. – axiac Apr 03 '18 at 23:41
  • @axiac i write first about adding @ on exact function but the i though also it will be helpful for him to understand that he can over ride all warning buy close it from error_reporting – Jehad Ahmad Jaghoub Apr 03 '18 at 23:46

2 Answers2

3

I would suppress the warning using @, and then see if you got any errors using socket_last_error, like so:

$success = @socket_connect($socket, $host, $port));
$err = socket_last_error($socket);
if ($err > 0) {
    $errormsg = socket_strerror($err);
    die("Couldn't create socket: [$err] $errormsg");
} else if (!$success) {
    die("Couldn't create socket: error unknown");
}
dave
  • 62,300
  • 5
  • 72
  • 93
0

Suppress the warning with @ operator. Note that you will need to check the return value of your socket_connect or anything else, to make sure an error hasn't occurred.

Another approach is to disable error display altogether, while retaining logging. See PHP's error handling.

Slavic
  • 1,891
  • 2
  • 16
  • 27