swiftsocket / SwiftSocket

The easy way to use sockets on Apple platforms
BSD 3-Clause "New" or "Revised" License
1.68k stars 400 forks source link

socket sends data, but never returns response #127

Open dcsim0n opened 6 years ago

dcsim0n commented 6 years ago

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?

Melf11 commented 6 years ago

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

dcsim0n commented 6 years ago

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 notifications@github.com 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.

sepandy commented 6 years ago

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

fukemy commented 5 years ago

same problem here, adding \n not working

ermanitu commented 5 years ago

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

Amin-Rehman commented 5 years ago

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

ermanitu commented 5 years ago

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