Thanks a lot for your help!
>> Looking at your code, I believe the delay is being triggered by your use of receiveMessage(…).>> TCP is not a message-oriented protocol, and thus receiveMessage(…) doesn’t make any sense [1]. What are the use-cases for "receiveMessage()" function?>> What you want is receive(minimumIncompleteLength:maximumLength:completion:).Oh yes, absolutely right! Thanks a lot!>> Note In iOS 13 beta and friends you can push a framer on to your NWConnection. >> The framer takes care of framing messages to the TCP connection and unframing messages from that connection,>> allowing you to work in terms of sending and receiving messages.This is really amazing and great! Really cool.
>> What exactly you mean with Network framework? An external lib or Core OS SDK?I think I found the first answer in the web [1]. IMHO you mean with Network framework classes from die SDK.E.g. NWConnection, NWEndpoint, NWParameters etc. (see prefix NW for Network Framework ^^)[1] https://developer.apple.com/documentation/network
class TcpSocket {
static let PORT: NWEndpoint.Port = 33120
var connection: NWConnection!
func connectToTcp(ipAddress: NWEndpoint.Host) {
let queue = DispatchQueue(label: "TCP Client Queue")
let tcp = NWProtocolTCP.Options.init()
tcp.noDelay = true
let params = NWParameters.init(tls: nil, tcp: tcp)
connection = NWConnection(to: NWEndpoint.hostPort(host: ipAddress, port: TcpSocket.PORT), using: params)
connection.stateUpdateHandler = { (newState) in
switch (newState) {
case .ready:
print("Socket State: Ready")
self.send()
self.receive()
default:
}
}
connection.start(queue: queue)
}
func receive() {
connection.receiveMessage { (data, context, isComplete, error) in
if (isComplete) {
print("Receive is complete, count bytes: \(data!.count)")
if (data != nil) {
print(data!.byteToHex())
} else {
print("Data == nil")
}
}
}
}
func send() {
let content: Data = „DummyData“.hexToData()
connection.send(content: content, completion: NWConnection.SendCompletion.contentProcessed(({ (NWError) in
if (NWError == nil) {
print("Data was sent to TCP destination ")
} else {
print("ERROR! Error when data (Type: Data) sending. NWError: \n \(NWError!)")
}
})))
}
}Here is my code: my problem is the receive code in line 29, here is my socket stopped and wait round about 8 seconds for the "socket received READ_CLOSE event". that takes pretty much 8-9 seconds every time. Can this timer be changed? Or maybe deactivated? I want the messages from the IP stack instantly not delayed.
Thanks a lot for your reply.>> In general, I’d recommend Network framework, especially if you’re working from SwiftWhat exactly you mean with Network framework? An external lib or Core OS SDK? I would like to use high level SDK classes, if it's possible. Something like NWConnection or there are better alternatives? And I would love to use Swift.>> There are two reasons for using older APIs:The mentioned two reasons from you (A) and (B) are not relevant for me, now.Only thing (B) could be relevant in the future, but not really now (so I'd put it in the back).>> With regards NO_DELAY, the snippet you posted, which sets the noDelay on NWProtocolTCP, is the correct way to handle that.Okay, thanks. Unfortunately I don't see any difference with and without the flag.I see the data in my log a few seconds later (after Wireshark). Any ideas? I could also post my code here.