How to listen on a network port in Objective-C

2019-03-16 18:51发布

问题:

I am trying to make an application for iPhone that can listen for traffick on a specific network port.

A server on my network is sending out messages (different status messages for devices the server handles) on a specific port.

My problem is that when I make a thread and makePairWithSocket I block the port for others who want to send messages to the server, so I only want to listen to the traffic on a specifyed port and then check for specific heraders and then use those messages. I know how to make the connection and talk to the server using write and read streams, but then I makePairWithSocket and block the port for all other devices on the network

Any one that has any suggestions on how to listen on a port in Objective-C without pairing with the server?

Thanks in advance Daniel

回答1:

Check out CocoaAsyncSocket. It gives you a nice and structured way (with delegates) to send and receive data... also with multiple clients. The documentation is quite good. project link

edit: Have a look at the AsyncUdpSocket class for a stateless UDP connection.



回答2:

I think this requires network support well below the socket API level, perhaps at the hardware driver level, assuming the packets are even being routed to your device.