views:

75

answers:

2

Hi! 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

A: 

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.

hotpaw2
I think he wants to capture a UDP broadcast packet, which can be done at the socket API level.
dreamlax
The server will send a response to that IP that sent the question, so I want to pair up and send a question then close connection and wait for an answer addressed to me. But if I stay paired the server wont send the answer cause I am blocking the port and it will think I am going to send more data.Can you accept "calls" without having the connection?
KungWaz
I can hard code the IP of the iPhone in the server so it send to that IP when there is a status change. But how do I accept the connection without pairing and blocking the port?
KungWaz
+1  A: 

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.

Felix
I have had a look at that project site before, but I can't really find anything that will do this without connecting to the server and blocking the port. Do you have any ideas on how to get around that with AsyncSocket?
KungWaz
found this: -acceptOnAddress:port:error:What do I create to use this? If I have a thread, how does it work? -acceptOnAddress:port:error: I guess this is what I'm after, to accept connections from a specific host on a specifi port, but how would I implement it so I can listen to it in the background?
KungWaz
A server can accept multiple connection on a single port. The server's AsyncSocketDelegate has the receive method "- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag;" where the "sock" parameter belongs to the client sending the data. You can store the "sock" parameter and use [sock writeData:] to send data to this specific client. So the port is not blocked. You do not need to disconnect any connection. Have a look at the examples provided with CococaAsyncSocket.
Felix
Where do I find the examples?
KungWaz
AsyncSocket *as = [[AsyncSocket alloc] initWithDelegate:self]; NSError *e;if([as acceptOnInterface:IP port:PORT error:else NSLog(@"Fail:%@", e);General CFsocket error is what I get when I use the above code, shouldn't that work?I make the connection and set up a socket to listen to, haven't set the delagets yet cause I want to make the connection work.
KungWaz