Search code examples
objective-cxcodetcpserverios12

How to create TCP IP Server using Objective-C on iOS


i have managed to create a client with objective-c where it can connect to any server, my app is running on an iPad.. so far a have tested the client on different servers telnet, nodejs, even a web based php server and it works fine. But my scope has changed as i need my app to act as the server hence instead of connecting to a given socket, it should first open a socket and wait for client connection and establish communication between the two.

Here is a snippet of how my client on the ipad connects to server:

NSLog(@"Setting up connection to %@ : %i", _ipAddressText.text, [_portText.text intValue]);
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (__bridge CFStringRef) _ipAddressText.text, [_portText.text intValue], &readStream, &writeStream);

messages = [[NSMutableArray alloc] init];

[self open];

I have tried cocoaAsyncSocket but um having trouble getting its work around: https://github.com/robbiehanson/CocoaAsyncSocket


Solution

  • There are better native alternatives to CocoaAsyncSocket now, namely Apple's Network Framework.

    Here is some nice sample code for a simple TCP client & server using the nw functions: https://developer.apple.com/documentation/network/implementing_netcat_with_network_framework