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.
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.