Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to detect closed socket #213

Open
Joebayld opened this issue Jun 20, 2022 · 3 comments
Open

Unable to detect closed socket #213

Joebayld opened this issue Jun 20, 2022 · 3 comments

Comments

@Joebayld
Copy link

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)")
                }
            }
@dannys42
Copy link
Contributor

dannys42 commented Jul 1, 2022

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

@Joebayld
Copy link
Author

@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.

@dannys42
Copy link
Contributor

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants