SwiftSocket icon indicating copy to clipboard operation
SwiftSocket copied to clipboard

socket sends data, but never returns response

Open dcsim0n opened this issue 7 years ago • 7 comments

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?

dcsim0n avatar Dec 27 '17 17:12 dcsim0n

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

Melf11 avatar Jan 24 '18 10:01 Melf11

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.

dcsim0n avatar Jan 24 '18 13:01 dcsim0n

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

sepandy avatar Aug 19 '18 01:08 sepandy

same problem here, adding \n not working

fukemy avatar Sep 20 '18 11:09 fukemy

Same problem here. Getting nil as response from read after a send.

ermanitu avatar Oct 07 '18 09:10 ermanitu

Having the same problem here. I can't even get 1 character to go through. :(

Amin-Rehman avatar Oct 10 '18 19:10 Amin-Rehman

I solved the problem. See here: https://github.com/swiftsocket/SwiftSocket/issues/157

ermanitu avatar Oct 11 '18 17:10 ermanitu