SwiftSocket
SwiftSocket copied to clipboard
socket sends data, but never returns response
I'm using the following code:
private func sendRequest(string: String, using client: TCPClient) -> String? {
appendToTextField(string: "Sending data ... ")
switch client.send(string: string) {
case .success:
return readResponse(from: client)
case .failure(let error):
appendToTextField(string: String(describing: error))
return nil
}
}
private func readResponse(from client: TCPClient) -> String? {
guard let response = client.read(1024*10) else { return nil }
return String(bytes: response, encoding: .utf8)
}
I've used a network traffic logger to verify that the request and response is indeed being sent. I even see the final ACK packet sent from host after the server sends it's FIN packet.
But, I only get a nil from readResponse, and if I return the error from client.read() i get the .unknownError. It's clear the client.connect() is a success because i can see the packets being transfered on the network, but I'm not getting the data out from client.read.
I had this working in ios 10.3, but now that i'm on ios11 and xcode9 it's all broke. Any ideas?
it might help you, i got a similar problem i could never send a second message and after hours of debugging i realized that i had to put a "return" ("\n") after every line i send and receive
Thanks for this, adding the timeout: 10 argument to both the write AND read fixed the issue in this case. Since iOS has added the nsurl tcp stream methods to the foundation kit I have migrated to that method since it allows threading the tcp communication.
Dana
Sent from my iPhone
On Jan 24, 2018, at 5:56 AM, Melf11 [email protected] wrote:
it might help you, i got a similar problem i could never send a second message and after hours of debugging i realized that i had to put a "return" ("\n") after every line i send and receive
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.
Even though I am setting timeout time for read 10 or higher I still don't get any data. I checked my server with telnet and it responded just fine
same problem here, adding \n not working
Same problem here. Getting nil as response from read after a send.
Having the same problem here. I can't even get 1 character to go through. :(
I solved the problem. See here: https://github.com/swiftsocket/SwiftSocket/issues/157