Mobile Device Sockets: Keep open vs reconnect on each request - sockets

I'm creating an application for a mobile device (Windows Phone 7) that opens a socket on a server.
Should I :-
Open the socket and hold it open for the application lifetime
Open and close on each request
I found this question IPC: Connect for each request or keep socket open? which is related but I wonder if the answer changes given the constraints on a mobile device

It depends.
If you don't need to hold the connection open to receive a message from the server then you may want to close it after you've finished using it.
You may, however, want to keep it open if you're making lots of requests in quick succession and the overhead of opening and closing the connection would cause an unwanted delay.
As a general rule for mobile app development is that you shouldn't use resources (including keeping connections open) for any longer than is absolutely necessary.

It depends on how your OS manages resources. If your app is the only one using the internet connection, then closing the socket could let the OS totally extinguish used network interface, which definitely will reduce power consumption.
Anyway, if you don't need to constantly send or receive something, I'd recommend you to close the socket.

Related

Correct usage of Appengine Sockets

I'm using the sockets api to communicate with the Apple Push Notification Service, although I'm not confident I'm using it correctly...
I have an initialization function where I establish my connection to APNS. However when is the correct time to call Close() on the connection? Or do I just leave it open and keep reusing it?
Thanks!
Apple specifically request that you don't close the connection with APNS and keep reusing it as long as possible.
Keep your connections with APNs open across multiple notifications; don’t repeatedly open and close connections. APNs treats rapid connection and disconnection as a denial-of-service attack. You should leave a connection open unless you know it will be idle for an extended period of time—for example, if you only send notifications to your users once a day it is ok to use a new connection each day.
(Source)
However, Apple will close the connection if you send them invalid data (such as notifications with invalid device tokens), so your code must be able to detect that and create a new connection when necessary. You should also read error responses from Apple, since those responses will let you know if you should re-send some of the notifications after you re-open the connection.
That's a good answer from Eran but you may also check out java-apns-gae.
It's an open-source Java APNS library that was specifically designed to work (and be used) on Google App Engine.
https://github.com/ZsoltSafrany/java-apns-gae

UDP sendto() No buffer space available

I'm trying to create an iOS application that sends data over UDP continuously over wifi/3G network.
I have an issue when I launch my app over 3G network after like 10 seconds I get this message :
sendto() : No buffer space available
It's not that a big deal because my app still works well BUT when I quit the app, I guess my buffer stays full because I can't use 3G anymore (I have to wait some time or reboot my phone)
Is there a way to flush this buffer before I quit my app ?
It sounds as if you're hitting the outbound bandwidth limit. If your app does this continuously while in use, isn't that going to make it very expensive for users to run? Most mobile users, I would guess, are on some kind of metered plan where they pay for transferred data.
I would guess that closing the socket normally before exiting should flush it first, since you've requested the data to be sent after all, but sometimes UDP sockets don't try very hard to deliver the data (since they are "lossy"), perhaps that's what's happening in your case too.

How to avoid double hand shake serverClient/clientServer connection

I have written a client server app that connect two iphones using bonjour. My problems is that when both devices start broadcasting and listening using bonjour they find each other at the same moment and both of them connect as a client of each other. How can I avoid this scenario? I am already checking if my app is connected as a server to the other app, but since that happens simultaneously I always connect as a server and client at the same time.
looking into ur case ; 1 solution is that u can make ur device on which app is hosted and gotta serve as server make that devices tcb as passive open so it can only listen on particular port/frequency and client can be programed to be in active open state so it can send request to server.

Apple Push Notification: Sending high volumes of messages

I am using PHP to connect to apns to send some notifications to multiple devices, although the question is more conceptual so it doesn't have to be specific to PHP. I will be sending to about 7000 devices (and growing) all at the same time. My process runs ONCE per day and broadcasts to all devices, so I am not constantly re-opening a connection.
open connection to apple
loop over device tokens
create payload aggregating all devices
end loop
write to socket ONCE with whole payload for 7000 devices
close connection
Can I do like above pseudo-code?
That is a correct approach but you need to check for APN Feedback and remove the "stale" devices. Apple will give you a list of tokens that they think are no longer valid. You should prune your database and never send to those tokens again.
Correct Approach here is,
you can open the connection.
perform as many writes as you like.
just make sure you check the connection status after each write
close the connection.
As each write is considered as a message specific to device, you can write one message at a time. But you can open a connection once and write as many you can.

Losing network connectivity on iPhone

I am developing a network application on iPhone that requires internet connection all the time. However, once I login to the server and keep the iPhone idle for a while, the iPhone goes to sleep mode and disconnects my network connection (it logs me out).
If I run the same application on iPhone, while the iPhone is connected to the PC through USB cable, it never loses its network connection.
In the info.plist file I have added these two flags, but does not seem to have any effect.
UIRequiresPersistentWifi -> true
SBUsesNetwork - integer ->3
Am I missing anything? Could you please let me know how can I make sure that the network connection is persistent throughout the life of the application?
In your application delegate ("appDelegate"), disable the idle timer in the +initialize method:
myApp.idleTimerDisabled = YES;
Note that this will keep your iPhone from sleeping while your app is open. This can present issues with battery life.
Another option might be to set up a background thread that opens a small CFStream on a timed basis.
What do you mean by "logs me out" here? At the network level, there is no "logged in" (*). There are only packets. You send them or you don't. So does your server process have some expectation of packets or messages arriving periodically? If it does, then you must send them, and that means that you can't go idle (idleTimerDisabled = YES). If you control the server, it is better to make it less demanding about how often you talk to it. This all happens well above the network layer, however.
UIRequiresPersistentWifi means that the Wifi radio is kept on while you're app is running, even if you don't talk on it. This is important for receiving data. Otherwise you drop off the network and others can't talk to you after about 30 minutes. It should be set in Info.plist, but this is certainly in your app bundle. If it weren't, your app wouldn't launch, so that isn't the problem.
(*) The cell network does have the concept of logged in, but that's not what's causing your problem.