how to make two tcp connectinos in ios app - ios

I am working on an ios app that needs to make tcp connections to two servers with different addresses. Does NSStream can make it, how to do that? shuold I put one communication on a separate thread?

Related

Framework for communicating between iOS and MacOS

I'm looking to build a pair of apps that work in a similar way to iTunes and the Remote app for iOS... Is there a framework for connecting iOS and MacOS over wifi?
Bluetooth is something I'd like to avoid and there's no need for a web service, I'm just looking to control my Mac app remotely from the phone.
Thanks in advance.
Id say the most ideal way to accomplish this is via the Bonjour protocol.
Here is a great article on the subject, http://mobileorchard.com/tutorial-networking-and-bonjour-on-iphone/.
Good thing with using Bonjour in iOS is it also works via Bluetooth.
Hope this helps !
here is an example project which exchange the data between iphone and iMAC with WiFi connectivity.
https://github.com/boobalaninfo/Bonjour-iOS-MAC-Apps
Use bonjour to search for devices. Then use CocoaAsyncSocket to send and receive data. It works like a charm.
Little info about AsyncSock:
GCDAsyncSocket and AsyncSocket are TCP/IP socket networking libraries.
Here are the key features available in both:
Native objective-c, fully self-contained in one class. No need to muck
around with sockets or streams. This class handles everything for you.
Full delegate support Errors, connections, read completions, write
completions, progress, and disconnections all result in a call to your
delegate method.
Queued non-blocking reads and writes, with optional timeouts. You tell
it what to read or write, and it handles everything for you. Queueing,
buffering, and searching for termination sequences within the stream -
all handled for you automatically.
Automatic socket acceptance. Spin up a server socket, tell it to
accept connections, and it will call you with new instances of itself
for each connection.
Support for TCP streams over IPv4 and IPv6. Automatically connect to
IPv4 or IPv6 hosts. Automatically accept incoming connections over
both IPv4 and IPv6 with a single instance of this class. No more
worrying about multiple sockets.
Support for TLS / SSL Secure your socket with ease using just a single
method call. Available for both client and server sockets.

To close NSStream objects before resigning active

I have an NSInputStream and NSOutputStream scheduled in a non-main-thread run loop. I'm using CFStreamCreatePairWithSocketToHost. The remote endpoint is an external device that communicates with the app with TCP over wifi.
When applicationWillResignActive is called, I want to close these. I want to be guaranteed that they will close, because another app may want to communicate with the same device, and the device only accepts one TCP connection.
Additional information:
It is a frequent occurrence in the context of this device that multiple apps may need to communicate with the device.
I have tried using performSelector:onThread:withObject:waitUntilDone:
(with waitUntilDone:YES) in applicationWillResignActive. Sometimes this doesn't work, and in those cases it seems that the selector does not get called.
My understanding is that I can't close the NSStream objects or destroy the task queue from the main thread.
I can not change how the device receives connections. It listens on one address/port for TCP connections, and allows only one connection (it doesn't multiplex connections by source port).
Use dispatch_sync() to the background queue.

Sync TCP Client in multi-client application without lags

I have application, whitch connents to N devices trought the network.
The problem is:
TCP Client class isn't async, I'm send the command to device, I want get answer now.
So, 2 or more active clients makes my app frozen.
Any ideas how to do some, that's will not freeze my application if many devices is enabled at one time?
TCP Client class uses Synapse classes package.
Thanks!
To avoid application hang, use a separate thread for every TCP client connection.
The thread then will run independently, connect to the device, and do the communication.
Note that the thread must not access objects/data in the main application thread without proper synchronization.

Is using a socket connection possible within iOS 7 background fetch mode?

Apple added in iOS 7 new possibility to execute activities in the background, fetch mode, which can be used e.g. for Twitter, when frequently small amounts of data need to be downloaded (official description: The app regularly downloads and processes small amounts of content from the network.),Apple Document Reference
However, I need to use a TCP connection.
Does anyone know whenever TCP connection may be used in such scenario between the calls?
The scenario is:
I open the TCP socket in the beginning and then the application can use it in these fetch calls. Of course handlers are needed when the TCP connection drops, but there is a difference between rare necessity to reconnect and constant (i.e. in each fetch iteration) need to reconnect.

Listening for a TCP connection in IOS 6

I am writing a client-server application for IOS6. I need to send simple messages from multiple iPhones to a single iPad. I have managed to get the send functions to work on the iPhone by using CFStreamCreatePairWithSocketToHost, by pointing to a realbasic application on the mac I see the messages.
I need to set up a socket to listen on a specific port on the iPad but seem to be having problems finding info and examples on this!
Does anyone have any simple examples of listening and reading sockets on IOS6?

Resources