如何识别 iOS 设备上收到的 udp 数据包的发送者 IP 地址?
如何找出运行 iOS 3.2
或更高版本的设备上收到的 udp 数据包的发送者 IP 地址?
例如,如果在计算机上使用 Python,我将使用 SocketServer.UDPServer,它采用 SocketServer.BaseRequestHandler 的子类,该子类定义了句柄打回来。当数据包到达时,回调将被调用,发送者的 IP 地址将位于 self.client_address
中,其中 self
是 SocketServer.BaseRequestHandler
实例。
不幸的是,我不知道在 Objective C 中编程并使用为 iOS 提供的框架时如何提取此信息或访问关联的 ip 标头。
现在我唯一的解决方案是在 udp 数据包的主体上显式写入发送者的 ip,但如果不需要的话,我宁愿不依赖发送者将该信息放在那里。
How to find out the sender's ip address of a udp packet that was received on a device running iOS 3.2
or higher?
For example, if using Python on a computer, I'd use SocketServer.UDPServer
which takes a subclass of SocketServer.BaseRequestHandler
which defines the handle
callback. When a packet arrives, the callback is called and the sender's ip address would be in self.client_address
, where self
is the SocketServer.BaseRequestHandler
instance.
Unfortunately, I don't know how to extract this information or gain access to the associated ip header when programming in Objective C and using the frameworks provided for iOS.
Right now the only solution I have is to explicitly write the sender's ip on the body of the udp packet, but I'd rather not rely on the sender putting that information there if I don't have to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有提到您使用什么 API 来接收数据包。不要将 IP 地址写入数据包本身,因为数据包在到达您之前可能会经过多次网络地址转换。
您可以使用常规 BSD 套接字来完成此任务。还有一些围绕 BSD 套接字的基础类,但我个人没有使用过它们,所以我不能给你一个像样的例子。
You didn't mention what API you are using to receive the packet. Don't write the IP address into the packet itself because the packet may go through a number of network address translations before it gets to you.
You can use regular BSD sockets to acheive this task. There are also a few Foundation classes that wrap around BSD sockets, but I haven't used them personally, so I can't give you a decent example.