swift: проблема с вызовом receiveMessage (завершение :) для UDP - PullRequest
0 голосов
/ 17 октября 2019

Я отправляю некоторые данные через UDP , но receiveMessage(completion:) не вызывается.

С Network Framework я открываю соединение, и когда онов состоянии готовности я отправляю одну дейтаграмму, которая успешно отправляется, но у меня проблема с получением входящей дейтаграммы. Я звоню приём по тому же соединению между прочим.

class func connect()
{
    connection = NWConnection(host: hostUDP, port: portUDP, using: .udp)
    connection?.stateUpdateHandler =
    {
        (newState) in switch (newState)
        {
        case .ready:
            //The connection is established and ready to send and recieve data.
            print("ready")
            self.sendPaket(self.sendingPacket)
            self.receive()
            print("i read this line")
        case .setup:
            //The connection has been initialized but not started
            print("setup")
        case .cancelled:
            //The connection has been cancelled
            print("cancelled")
        case .preparing:
            //The connection in the process of being established
            print("Preparing")
        default:
            //The connection has disconnected or encountered an error
            print("waiting or failed")
        }
    }
    connection?.start(queue: .global())
}

class func sendPaket(_ packet:String)
{
    let packet = dataWithHexString(hex: sendingPacket)
    print("converted version to byte is :\(packet)")
    connection?.send(content: packet, completion: NWConnection.SendCompletion.contentProcessed((
        {
            (NWError) in
            if (NWError != nil)
            {
                print("error in sending packet : \(NWError!)")
            }else
            {
                print("Packet Sent Successfully")
            }

    })))

}

class func receive()
{
    print("Receive func got called")
    connection?.receive(minimumIncompleteLength: 17, maximumLength: 100, completion:(
    {
        (data, context, isComplete, error) in
        print(context!)
        print("receiveMessage called")
            if (isComplete)
            {
                print("receiving is complete!")
                if (data != nil)
                {
                    let backToString = String(decoding: data!, as: UTF8.self)
                    print("Received message: \(backToString)")
                }else
                {
                    print("data is nil")
                }
            }else
            {
                print("isn't complete")
            }
            if error != nil
            {
                print("error in receiving : \(error!)")
            }
    }))
}

Что мне делать?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...