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.