ios4 - Transmitting a string via UDP using cocoaasyncsocket -
i've been teaching myself objective-c on past few months; i'm building iphone app company. started (and still am) complete novice, until have had no problems finding answers questions @ various locations online.
for final, , important, piece of app, need send simple string address/port via udp when button pressed. string, address, , port variables pulled object passed view controller.
i have been digging around 2 days looking @ solutions , reading examples, reads greek me. i'm not sure major hunk of knowledge seem have missed out on, i'm @ total loss. learned cocoaasyncsocket
, , how "simple" is, , sounds perfect need, can't seem wrap mind around it. i'm hoping here can break down me simple terms.
here snippet of code i've been trying, no luck. code viewcontroller
, asyncudpsocket.h
imported:
-(ibaction)udpbuttontwopressed:(id)sender { nsdata *mydata; mydata = [[nsdata alloc] initwithdata:([selectedobject valueforkey:@"udpcommandtwo"])]; asyncudpsocket *mysocket; mysocket = [[asyncudpsocket alloc] initwithdelegate:self ]; nserror *error = nil; if (!([mysocket connecttohost:([selectedobject valueforkey:@"serveripaddress"]) onport:([[selectedobject valueforkey:@"serverport"] intvalue]) error:&error])) { nslog(@"can't connect cause: %@", error); abort(); } [mysocket close]; [mysocket release]; [mydata release]; }
what doing wrong here?
there 2 things stand out example.
i don't see writing socket. have @ writedata:withtimeout:tag.
cocoaasyncsocket asynchronous in example going out of scope. if wanted write synchronously there example in reference.
nsstring *customrunloopmode = @"mysyncwrite";
[asyncsocket addrunloopmode:customrunloopmode]; [asyncsocket writedata:didbackgrounddata withtimeout:timeout_none tag:tag_bg];
syncwritecomplete = no; bool runloopready = yes;
while (runloopready && !syncwritecomplete) { runloopready = [[nsrunloop currentrunloop] runmode:customrunloopmode beforedate:[nsdate datewithtimeintervalsincenow:1.0]]; }
[asyncsocket removerunloopmode:customrunloopmode];
hth
Comments
Post a Comment