SwiftSocket icon indicating copy to clipboard operation
SwiftSocket copied to clipboard

UDP receive problem

Open TheRealDoubleB opened this issue 7 years ago • 2 comments

Hi! I'm writing an App, that sends a specific String via UDP broadcast to the local network. On other machines in the network there are running UDP clients (c++) which respond to that specific String. So I can "scan" the network for my C++ Servers.

Sending the Broadcast with UDPClient works good. The Servers are also responding correctly. I am using the UDPClient to receive the responds in a loop. All that is done in a background task via DispatchQueue.async(). Problem: After all Messages have been received, on the next call of function recv() of UDP client just exits my thread without any notice. I traced it back until recvfrom() method in yudosocket_recive().

Any idea what that could be?

here my code:

private func readResponse(from client: UDPClient, completed: @escaping (ResponseData) -> Void)-> String? {
        var stringResponse: String = ""
        var count: Int = 0
        repeat{
            let response = client.recv(2048)
            let tempResponse = String(data: Data(bytes: response.0!), encoding: .utf8)
            completed(ResponseData(data: tempResponse!, state: ResponseState.Success))
            stringResponse += tempResponse!
            count = (response.0?.count)!            
        }while count > 0
        client.close()        
        return stringResponse
 }

(this function is called after sending the Broadcast with the given UDPClient)

TheRealDoubleB avatar Sep 04 '17 12:09 TheRealDoubleB

I have problem too :(

J-Arji avatar Oct 19 '17 11:10 J-Arji

Hi @TheRealDoubleB were you able to send a broadcast and get a response ? can you explain or show how were you able to do so using swiftsocket ? thank you

soufianeEssabbane avatar Feb 28 '19 22:02 soufianeEssabbane