CAsysncSocket-udp tool-AsyncUdpSocket usage.

Source: Internet
Author: User

The programming of the AsyncSocket network library under the iPhone is described in this article. The CFNetwork C library programming is recommended for the iphone standard, but programming is quite annoying. In other operating systems, classes are often used to encapsulate Socket functions. For example, the CAsysncSocket of MFC also has a similar open-source project on the iphone. cocoa AsyncSocket Library


Http://code.google.com/p/cocoaasyncsocket/

Here we only use the example above. You can use it in your project. If you have any questions, please leave a message to discuss it.

Import AsyncUdpSocket. h AsyncUdpSocket. m to your project,

In your own util, add the code


[Cpp]
01. <PRE class = cpp name = "code"> # pragma mark udp
02.-(void) sendSearchBroadcast {
03. NSString * bchost = @ "broadcast 255"; // broadcast is sent here.
04. [self sendToUDPServer: @ "hello udp" address: bchost port: BCPORT];
05 .}
06.-(void) sendToUDPServer :( NSString *) msg address :( NSString *) address port :( int) port {
07. AsyncUdpSocket * udpSocket = [[AsyncUdpSocket alloc] initWithDelegate: self] autorelbid]; // obtain udp util
08. NSLog (@ "address: % @, port: % d, msg: % @", address, port, msg );
09. // receiveWithTimeout is necessary or you won't receive anything
10. [udpSocket receiveWithTimeout: 10 tag: 2]; // set timeout to 10 seconds.
11. [udpSocket enableBroadcast: YES error: nil]; // if you send a broadcast, enableBroadcast must be enabled first.
12. NSData * data = [msg dataUsingEncoding: NSUTF8StringEncoding];
13. [udpSocket sendData: data toHost: address port: port withTimeout: 10 tag: 1]; // send udp
14.} </PRE> <PRE class = cpp name = "code"> // The following are the sent callback functions.
15.-(BOOL) onUdpSocket :( AsyncUdpSocket *) sock didReceiveData :( NSData *) data withTag :( long) tag fromHost :( NSString *) host port :( UInt16) port {
16. NSString * rData = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]
17. autorelease];
18. NSLog (@ "onUdpSocket: didReceiveData: --- % @", rData );
19. return YES;
20 .}
21.-(void) onUdpSocket :( AsyncUdpSocket *) sock didNotSendDataWithTag :( long) tag dueToError :( NSError *) error {
22. NSLog (@ "didNotSendDataWithTag ----");
23.
24 .}
25.-(void) onUdpSocket :( AsyncUdpSocket *) sock didNotReceiveDataWithTag :( long) tag dueToError :( NSError *) error {
26. NSLog (@ "didNotReceiveDataWithTag ----");
27 .}
28.-(void) onUdpSocket :( AsyncUdpSocket *) sock didSendDataWithTag :( long) tag {
29. NSLog (@ "didSendDataWithTag ----");
30 .}
31.-(void) onUdpSocketDidClose :( AsyncUdpSocket *) sock {
32. NSLog (@ "onUdpSocketDidClose ----");
33. }</PRE> <BR>
34. <PRE> </PRE>
35. OK. Give it a try.
36. <P> </P>
37. <P> </P>
38. <PRE> </PRE>
39. <PRE> </PRE>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.