Sending UDP packets in Objective C: packet arriving without any data -


i'm trying send udp packets in objective c. more specifically, building xcode targeting iphone 6.1 simulator.

i can't seem receive data send. weirdly, data event... data's been truncated length 0.

i've cut down as can, make dead simple test think should pass.

#import "udpsockettest.h" #include <arpa/inet.h> #import <corefoundation/cfsocket.h> #include <sys/socket.h> #include <netinet/in.h>  @implementation udpsockettest  static int receivedbytecount = 0; void onreceive(cfsocketref socket, cfsocketcallbacktype type, cfdataref address, const void *data, void *info); void onreceive(cfsocketref socket, cfsocketcallbacktype type, cfdataref address, const void *data, void *info) {     // gets called once, cfdatagetlength(data) == 0     receivedbytecount += cfdatagetlength(data); }  -(void) testudpsocket {     struct sockaddr_in addr;     memset(&addr, 0, sizeof(addr));     addr.sin_len = sizeof(addr);     addr.sin_family = af_inet;     addr.sin_port = htons(5000); // <-- doesn't matter, not sending receiver     inet_pton(af_inet, "127.0.0.1", &addr.sin_addr);     cfsocketcontext socketcontext = { 0, (__bridge void*)self, cfretain, cfrelease, null };      // prepare receiver     cfsocketref receiver = cfsocketcreate(kcfallocatordefault, pf_inet, sock_dgram, ipproto_udp ,kcfsocketdatacallback, (cfsocketcallback)onreceive, &socketcontext);     cfrunloopaddsource(cfrunloopgetcurrent(), cfsocketcreaterunloopsource(null, receiver, 0), kcfrunloopcommonmodes);     cfsocketconnecttoaddress(receiver, cfdatacreate(null, (unsigned char *)&addr, sizeof(addr)), -1);      // point sender @ receiver     cfsocketref sender = cfsocketcreate(kcfallocatordefault, pf_inet, sock_dgram, ipproto_udp, kcfsocketdatacallback, (cfsocketcallback)onreceive, &socketcontext);     cfrunloopaddsource(cfrunloopgetcurrent(), cfsocketcreaterunloopsource(null, sender, 0), kcfrunloopcommonmodes);     cfsocketconnecttoaddress(sender, cfsocketcopyaddress(receiver), -1);      // send data of sixty zeroes, allow processing occur     cfsocketsenddata(sender, null, (__bridge cfdataref)[nsmutabledata datawithlength:60], 2.0);     [[nsrunloop currentrunloop] rununtildate:[nsdate datewithtimeintervalsincenow:1.0]];      // did data arrive?     stasserttrue(receivedbytecount > 0, @"");     // nope } @end 

what doing wrong? know it's obvious, can't see it.

i'm surprised you're getting callbacks @ all—you're never binding receiver local port. you're creating 2 sockets , telling both of them "i went send data 127.0.0.1:5000", saying "i want receive data on 127.0.0.1:5000.

in order that, should calling cfsocketsetaddress on receiver socket, not cfsocketconnecttoaddress. equivalent calling bind(2) system call on underlying native bsd socket.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -