BlueSocket icon indicating copy to clipboard operation
BlueSocket copied to clipboard

Unable to detect closed socket

Open Joebayld opened this issue 3 years ago • 3 comments

I'm not reciving zero bytes when the socket loses connection. Could something have changed regarding this?

It used to return bytesRead = 0 when the connection was closed.

            socketQueue.async { [weak self] in
                var readData = Data()
                var shouldKeepRunning = true
                do {
                    repeat {
                        let bytesRead = try socket.read(into: &readData)
                        if bytesRead > 0 {
                            let readString = String(data: readData, encoding: .utf8) ?? ""
                            self?.gotInput(string: readString)
                            readData = Data()
                        } else {
                            shouldKeepRunning = false
                            break
                        }
                    } while shouldKeepRunning

                    // close socket when bytesRead is zero
                    socket.close()
                    self?.connectionClosed()
                } catch {
                    self?.status = .error(message: error.localizedDescription)
                    log.error("Read error: \(error). Read data: \(readData)")
                }
            }

Joebayld avatar Jun 20 '22 19:06 Joebayld

Nothing should have changed here. What is bytesRead returning instead?

dannys42 avatar Jul 01 '22 00:07 dannys42

@dannys42 bytesRead isn't getting returned anything. The socket is on Wi-Fi and for a test, I turn off Wi-Fi on my computer (to force a disconnect), but nothing happens.

Joebayld avatar Oct 26 '22 08:10 Joebayld

@Joebayld what do you mean? Is it blocking? Or is it returning with a 0 or -1? Can you setup an example program to demonstrate this?

dannys42 avatar Oct 27 '22 15:10 dannys42