4

When I am connected to internet then It works perfect but when Internet is not connected then I go error on 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 am intentionally not connected to internet and i want to show alertView in iOS app to user when user is not conneted to internet that :

You are not connected to internet

Instead of

Warning : stream_socket_client( ) , php_network_getaddresses getaddrinfo failed nodename nor serv name provided or notknown

So how can i handle that 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 will be appreciated.

Ponting
  • 2,248
  • 8
  • 33
  • 61
  • don't you want the Iphone/Pad to be not connected to the internet, not the server your pinging? – DevZer0 Aug 08 '13 at 08:55
  • @DevZer0 : Obviously I want. But when it is not connected to internet then i want to throw error. – Ponting Aug 08 '13 at 08:58
  • 1
    So why does PHP get involved when you really need is a state handler from IOS – DevZer0 Aug 08 '13 at 08:59
  • @DevZer0 : I mistakenly told you in wrong way..sorry for that.. In my case,iPhone is connected to internet but when server is down for any reason then I want to throw error or handle that error and displayed that error to iPhone. – Ponting Aug 08 '13 at 09:02
  • It sounds like what you want to do is make the iOS app that's connecting to your server display an error to the user when the connection fails? Am I understanding tha correctly? – Carl Veazey Aug 10 '13 at 22:19
  • @CarlVeazey :Yes,Exactly..When Server failed to connect to internet then display Alert to user. – Ponting Aug 13 '13 at 12:32

2 Answers2

2

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

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

You will be notified if the socket has an error with:

case NSStreamEventErrorOccurred:

or

case NSStreamEventEndEncountered:

In which case you can show the network error to your user. If however they lose internet connection you won't know until you try to send some data over the socket. So to get around this problem you should implement Reachability.

Github project

or the Apple helper class.

Apple Reachability

You will be notified when there is no connection as well as when there is a connection allowing you to notify the user as needed.

Hope I understood correctly.

sbarow
  • 2,759
  • 1
  • 22
  • 37
  • Do i have to set any delegate to call this method? – Ponting Aug 16 '13 at 18:55
  • Yes, the class that creates the socket can handle the delegate methods. @Ponting reference doc for you. http://developer.apple.com/library/ios/documentation/cocoa/reference/NSStreamDelegate_Protocol/Reference/Reference.html – sbarow Aug 16 '13 at 19:03
  • I used this method but it never class . I dont understand the reason of it. – Ponting Aug 16 '13 at 19:11
  • I also have implemented NSStreamDelegate Protocol. – Ponting Aug 16 '13 at 19:12
  • Can you post the code where you are setting up your NSStreams. – sbarow Aug 16 '13 at 19:24
  • Are you using NSStream's to connect to your server? http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSStream_Class/Reference/Reference.html – sbarow Aug 17 '13 at 08:42
  • I am using NSURLConnection to connect to server. – Ponting Aug 17 '13 at 08:56
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35646/discussion-between-sbarow-and-ponting) – sbarow Aug 17 '13 at 09:08
1

Wait... so in your scenario, the iPhone app is the "socket server", relative to which PHP is a "socket client". Am I getting this right?

Assuming so... You've already silenced the error message with "@". The only thing remaining is that you simply check if the return value is false, e.g.

$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 could also use set_error_handler() right before the call, and handle the error there if it occurs, and finally restore the error handler to its previous state.

boen_robot
  • 1,450
  • 12
  • 24
  • I got error at this line : $this->smtp_conn = @stream_socket_client($host.":".$port, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $socket_context)..So code after this line won't be executed. So your solution after this line has no meaning..Can you elaborate how can set_error_handler() solves the problem ? Thanks. – Ponting Aug 16 '13 at 18:37
  • If it's a fatal error, then set_error_handler() won't help you recover from it, but it will at least let you end with a custom message as output. And if it's a non-fatal error (i.e. a "WARNING", as per your original post), then the following line should be executed regardless, and thus the above would work. – boen_robot Aug 16 '13 at 19:41