Skipping a command when app is freezing - iphone

How can I handle the problem when in my app somebody set a wrong IP address to socket communication?
The app is freezing at line, where the socket is waiting for answer from the server. (i'm using SmallSockets)
Is it posible to make a timer for this command, and after the timer count down skip this command?

You shouldn't be handling synchronous network communications on the main thread. You should be doing networking stuff asynchronously. Here's an example that should get you going.
Using an asynchronous approach, you should be able to cancel a network operation after a timeout easily.

Related

How to detect when socket connection is lost?

I have a script (I don't have the code example here at the moment but I used IO::Async) which connects to socket on a remote server and listens. Client usually just listens for new data.
Problem is that the client is not able to detect if network problems occur and the socket connection is gone.
I used IO::Async and I also tried it with IO::Socket. Handle is always "connected" after the initial connection is established.
If the network connection is established again the socket connection is naturally still lost because the script has no idea that it should reconnect.
I was thinking to create some kind of "keepAlive" which "pings" (syswrite) the socket every X seconds (if nothing new came through socket) to check whether the connection is still there.
Is this the correct way to do it or is there maybe an another more creative or cleaner solution?
You can set the SO_KEEPALIVE socket option which, for TCP, sends periodic keepalive messages, and may help detect this condition. If this is detected, you will be delivered an EOF condition (most likely causing the containing IO::Async::Stream to fire on_read_eof).
For a better solution you might consider some sort of application-level keepalive message, such as IRC's PING command.
The short answer is there is no default way to automatically detect a dropped socket in perl.
Your approach of pinging would probably work pretty well; you could run a continuous thread in the background that sends ping requests and if it doesn't receive a response the main thread can be notified and a reconnect should be issued.
If you want to get messy you can work with select() to detect keep alive messages; however this may require some OS configuration depending upon your platform.
See this thread for more details: http://www.perlmonks.org/?node_id=566568

Send data to socket on iPhone

I don't know very much about sockets at all. What I am wondering is how the sockets work.
1) Firewalls often block ports. Is it plausible that if I request a socket and it is behind a blocked port anything that is sent or received will get intercepted and destroyed? Is this an issue?
2) On the server side how do you keep the connection alive to send data back through, like if I was building a chat app is it possible to start a connection through a socket and keep that alive and have the server basically push new data through the stream. Instead of the apps having to query every so often.
3) If the app goes into the background apple says they will keep the socket as long its not needed (if it is voip). Does that mean that I could still send data through to the app and have it handled? If my app is registered for location updates and executing in the background already will the socket stay open?
Are there any server languages that make it simple? I am currently using python and Django for a simple http server. Are any tutorials on setting up a server that can keep connections alive. I really don't know much about this so what i'm asking may not make any sense but some direction would be greatly appreciated.
1) I haven't found an issue with firewalls at all.
2) I used a twisted server along side my web server to implement a event driven socket server and it works great.
3) the sockets will stay open as long as you are executing in the background and the delegate methods are called to handle stream events.
There is an excellent tutorial here:
http://www.raywenderlich.com/3932/how-to-create-a-socket-based-iphone-app-and-server
that goes over the twisted framework and how to create a chat app with sockets. I found that immensely helpful.

How to run CFSocket on a background thread for more accurate ping time?

Background...
I am modifying Apple’s SimplePing example to do an ICMP ping for an iPhone app. The code wraps a native socket in a CFSocket object specifying a read callback, then adds it as a run loop source on the main thread. When a packet is sent to the socket, the callback is invoked to time the round trip, verify the contents, update the UI, etc.
Question...
What would be the best approach for moving this processing to a background thread so the ping time is as accurate as possible? I need to measure the precise time between the call to the socket “sendto” method and the callback invocation without interruption.
Any examples or pseudo code would be extremely helpful. I have done a lot of reading on threading in Cocoa (NSThread vs. NSOperation, NSRunLoop, etc.), but so far, I can’t quite piece it all together.
Thanks
Do you need to support iOS 3.x? If not, you could look into using Grand Central Dispatch; in this scenario, you would specify the socket as a source for a dispatch queue and give it the highest priority.

ConnectEx with IOCP problem

I've made a simple dummy server/dummy client program using IOCP for some testing/profiling purpose. (And I also wanted to note that I'm new to asynchronous network programming)
It looks like the server works well with original client, but when the dummy client tries to connect to the server with ConnectEx function, IOCP Worker thread still gets blocked by GetQueuedCompletionStatus function and never returns result while the server succeeds in accepting the connection.
What is the problem and/or the reason, and how should I do to solve this problem?
I think you answer your own question with your comment.
Your sequence of events is incorrect, you say that you Bind, ConnectEx, Associate to IOCP.
You should Bind, associate the socket with the IOCP and THEN call ConnectEx.
Even after you associate your accepted socket to IOCP, your worker thread will remain blocked on GetQueuedCompletionStatus untill you post an "unlocking" completion event.
Completion events for receive/write operation wo'nt be sent by the system unless you "unlock" your new socket.
For details ckeck the source code of Push Framework http://www.pushframework.com It is a C++ network application framework using IOCP.
The "unlocking" trick exists in the "IOCPQueue" class.

what is blocking and non-blocking web server, what difference between both?

I have seen many a web framework provide a non-blocking web server, I just want to know what it means.
a blocking web-server is similar to a phone call. you need to wait on-line to get a response and continue; where as a non-blocking web-server is like a sms service. you sms your request,do your things and react when you receive an sms back!
Using a blocking socket, execution will wait (ie. "block") until the full socket operation has taken place. So, you can process any results/responses in your code immediately after. These are also called synchronous sockets.
A non-blocking socket operation will allow execution to resume immediately and you can handle the server's response with a callback or event. These are called asynchronous sockets.
Non-blocking generally means event driven, multiplexing all activity via an event driven system in a single thread, as opposed to using multiple threads.