I use this library: https://github.com/robbiehanson/CocoaAsyncSocket
My test code:
#import <UIKit/UIKit.h>
@class GCDAsyncUdpSocket;
@interface ThirdViewController : UIViewController
{
GCDAsyncUdpSocket *udpSocket;
}
.m:
#import "ThirdViewController.h"
#import "DDLog.h"
#import "DDTTYLogger.h"
#import "GCDAsyncUdpSocket.h"
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
@implementation ThirdViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[DDLog addLogger:[DDTTYLogger sharedInstance]];
udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
[udpSocket bindToPort:7744 error:nil];
[udpSocket beginReceiving:nil];
[udpSocket enableBroadcast:YES error:nil];
//[udpSocket connectToHost:@"127.0.0.1" onPort:55544 error:nil];
NSData *data = [@"test" dataUsingEncoding:NSUTF8StringEncoding];
//[udpSocket sendData:data withTimeout:-1 tag:0];
[udpSocket sendData:data toHost:@"127.0.0.1" port:55544 withTimeout:-1 tag:0];
}
But the package is not sent. I used this packet sniffer: http://sourceforge.net/projects/packetpeeper/
In the library there is an example of a client (for Mac), his packages I see. I tried to run the application on a real device, but nothing was sent, too (address, of course, was the other). What is the problem?
You’re listening on port
7744and you’re transmitting to port55544. You should be transmitting to the same port as you’re listening at. i.e. the send should be:An educated guess as to the reason packetkeeper isn’t seeing the packets is because they are not going out any network interface – it’s a general optimization that packets for the
lodevice (127.0.0.1) do not go out on the wire; they are just looped around locally. Most packet interception devices cannot detect packets of this type.Edit You need at least two
GDCAsyncUdpSockets – one for the client, one for the server. The easiest way to accomplish this is to have two separate applications, one running as the server, the other running as the client.This ThirdViewController is a UDP client of some form. When you issue the bindToPort, you need to use the port number
0, this causes the OS to allocate a port for this system which is used for listening. This is needed if you ever want to receive a packet sent from the server.as an aside, I would always check for an error on bindToPort.
On the server side, you need to bind to a known port:
On the client side, when you send a message you need to do (for the simulator):
On the server side, when you receive the message in the
didReceiveDatahandler, you are passed afromAddress. This address corresponds to the address and port fromudpSocket, and if you want to transmit a message back to the client, you can use:I have had no difficulties sending packets to the
serverSocketfrom theudpSocketand processing responses when I followed this methodology. I use wireshark on the Mac side to see packets coming from the iDevice. When I used the simulator, I sent to 127.0.0.1 and saw the responses on the server.