2

Is there a way to flush the internal buffers/queues/etc. of CocoaAsyncSocket (GCDasyncSocket)?

I want to setup the environment that when I call the [... readDataWithTimeout ..] method, I don't want it to read any left over garbage data from a previous connection that might have been buffered and sitting somewhere.

Or sometimes I just want to ignore a stream of data and start over, so want to ensure a clean pipe.

Sebastian Dwornik
  • 2,526
  • 2
  • 34
  • 57

2 Answers2

0

Make sure you send \n or \r after you write data. I have write a socket manager class you can check it here: CocoaAsyncSocketManager

Sample code:

func sendMessage(messageString msg: String) {
    if let _data = msg.data(using: .utf8), let _carriageReturn = "\r".data(using: .utf8) {
        tcpSocket?.write(_data, withTimeout: -1, tag: 0)
        tcpSocket?.write(_carriageReturn, withTimeout: -1, tag: 99)
    }
}
Adriatik Gashi
  • 332
  • 4
  • 10
0

I've also asked this question in the CocoaAsyncSocket Google Group and Robbie Hanson was kind enough to elaborate with his answer.

Sebastian Dwornik
  • 2,526
  • 2
  • 34
  • 57