I have a iOS client application that creates NWConnection over TCP.
connection = NWConnection(host: hostTCP, port: portTCP, using: .tcp)
connection?.stateUpdateHandler =
{ [self] (newState) in
print("This is stateUpdateHandler:")
switch (newState)
{
case .ready:
print("State: Ready\n")
self.sendTCP(messageToTCP)
self.receiveTCP()
case .setup:
print("State: Setup\n")
case .cancelled:
print("State: Cancelled\n")
case .preparing:
print("State: Preparing\n")
default:
print("ERROR! State not defined\n")
}
}
print("connectToTCP: Connection to server started\n")
connection?.start(queue: .global())
And the receiveTCP() function is as follows:
func receiveTCP()
{
connection?.receiveMessage
{ (data, context, isComplete, error) in
if (isComplete)
{
print("Receive is complete")
if (data != nil)
{
let backToString = String(decoding: data!, as: UTF8.self)
print("Received message: \(backToString)")
}
else
{
print("Data == nil")
}
}
else
{
self.receiveTCP()
}
}
}
I am using a command line tool as a server application running on my macBook which listens to the incoming connection and sends/receives messages. Sending messages works fine from the iOS client application but the receive doesn't work. Also the client app works (both send and receive) fine over UDP. Is there anything wrong i am doing here?
In Apple documentation in https://developer.apple.com/documentation/network/nwconnection/3020638-receivemessage
its mentioned
Receiving messages allows you to deal with complete datagrams or application-layer messages without needing to reconstruct a stream. If you are using UDP, receiving a message will deliver a single datagram. If you request to receive a message on a protocol that is otherwise an unbounded bytestream, like TCP or TLS, note that this will not deliver any data until the stream is closed by the peer.
What does this mean "If you request to receive a message on a protocol that is otherwise an unbounded bytestream, like TCP or TLS, note that this will not deliver any data until the stream is closed by the peer."?
Does anyone know of a simple TCP example for iOS devices that uses NWFramework to send and receive string data to and from a server?