gpt4 book ai didi

ios - 接收 UDP 消息

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:57:03 26 4
gpt4 key购买 nike

我在特定端口上接收消息时遇到问题。发送工作正常。

代码看起来像这样:

import Foundation
import CocoaAsyncSocket


class InSocket: NSObject, GCDAsyncUdpSocketDelegate {

//let IP = "192.168.1.196"
let PORT:UInt16 = 14000
var isocket:GCDAsyncUdpSocket!

override init(){
super.init()
setupConnection()
}

func setupConnection(){

isocket = GCDAsyncUdpSocket(delegate: self, delegateQueue: DispatchQueue.main)
do{
try isocket.bind(toPort:PORT)
try isocket.beginReceiving()
} catch {print("ErrorReceive")}
}


func udpSocket(sock: GCDAsyncUdpSocket!, didReceiveData data: NSData, fromAddress address: NSData!, withFilterContext filterContext: AnyObject!) {
let str = NSString(data: data as Data, encoding: String.Encoding.ascii.rawValue)
print(str)

}
}

我在网络统计中看到应用程序正在接收数据包: Packets

但我在控制台中没有看到任何内容。一些想法?

最佳答案

在委托(delegate)函数前添加:func udpSocket(....) 那个属性

@objc(udpSocket:didReceiveData:fromAddress:withFilterContext:)

XCode 8 会发出警告,但不用注意,UDP 数据包会到达。

关于ios - 接收 UDP 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39882633/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com