Warning: stream_socket_client (), php_network_getaddresses getaddrinfo nodename failed or service name provided or unknown

When I am connected to the Internet, then it works fine, but when the Internet is not connected, I go by mistake on the following lines:

$socket_context = stream_context_create($options); $this->smtp_conn = @stream_socket_client($host.":".$port, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $socket_context); 

I'm intentionally not connected to the Internet, and I want to show alertView in an iOS application to a user when the user is not connected to the Internet, which:

You are not connected to the Internet

Instead

Warning: stream_socket_client (), php_network_getaddresses getaddrinfo nodename failed or service name provided or unknown

So how can I handle this error?

 // -------------- Code where I am setting NSStream in .m file :---------- #import "LoginViewController.h" // --------------- here I set the delegate ------------- @interface LoginViewController () <NSStreamDelegate> -(void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode { } 

Any help would be appreciated.

+4
source share
2 answers

Assuming you are using NSStream to connect your sockets. In delegate method

 -(void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode 

You will be notified if the socket has an error:

 case NSStreamEventErrorOccurred: 

or

 case NSStreamEventEndEncountered: 

In this case, you can show a network error for your user. If, however, they lose their Internet connection, which you will not recognize until you try to send some data through the socket. Therefore, to get around this problem, you must realize the possibility of achieving results.

Github project

or Apple's assistant class.

Apple Achievement

You will be notified when there is no connection, and also when there is a connection that allows you to notify the user as necessary.

I hope I understood correctly.

+2
source

Wait ... so in your scenario, the iPhone application is a "socket server", regarding which PHP is a "socket client". Do I understand this correctly?

Assuming ... You have already disabled the error message using "@". All that remains is that you are simply checking to see if the return value is false, for example.

 $socket_context = stream_context_create($options); $this->smtp_conn = @stream_socket_client($host.":".$port, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $socket_context); if (false === $this->smtp_conn) { //Handle the error here echo 'You are not connected to internet'; } 

If using the @ operator makes you feel dirty (as it should), you can also use set_error_handler () right before the call and handle the error there if that happens, and finally restore the error handler to its previous state.

+1
source

Source: https://habr.com/ru/post/1495907/


All Articles