iOS Development UDP protocol send broadcast find device

Source: Internet
Author: User

Code link Http://i.cnblogs.com/EditPosts.aspx?opt=1

#import "AsyncUdpSocket.h"

#import <ifaddrs.h>

#import <arpa/inet.h>

Make a UDP request

-(void) makeudp

{

Instantiation of

Asyncudpsocket *socket=[[asyncudpsocket alloc]initwithdelegate:self];

Start the local port

[Socket LocalPort];

Nstimeinterval timeout=1000;//Send Timeout time

NSString *[email protected] "Dongqiangfei";//content sent to the server

NSData *data=[nsdata datawithdata:[request datausingencoding:nsasciistringencoding];

UInt16 port=6000;//Port

Nserror *error;

Send broadcast settings

[Socket Enablebroadcast:yes error:&error];

@ "10.10.60.255"

Change the final number of the target IP to 255 (meaning search All)

Nsarray *strarr=[[self getipaddress] componentsseparatedbystring:@ "."];

Nsmutablearray *muarr = [Nsmutablearray Arraywitharray:strarr];

[Muarr Replaceobjectatindex: (strarr.count-1) withobject:@ "255"];

NSString *finalstr = [Muarr componentsjoinedbystring:@ "."];/ /Destination IP

/*

Send Request

SendData: What to send

Tohost: IP of the target

Port: Port number

Timeout: Request timed out

*/

BOOL _isok = [socket senddata:d ata tohost:[nsstring stringwithformat:@ "%@", Finalstr] port:port withtimeout:timeout tag : 1];

if (_isok) {

UDP request Succeeded

}else{

UDP request failed

}

[Socket receivewithtimeout:1000 tag:0];//start receive thread-n? seconds timeout

NSLog (@ "start");

}

Receive information

-(BOOL) Onudpsocket: (Asyncudpsocket *) sock didreceivedata: (NSData *) data withtag: (long) tag fromhost: (NSString *) host Port: (UInt16) port{

nsstring* result;

result = [[NSString alloc] Initwithdata:data encoding:nsasciistringencoding];

NSLog (@ "%@", result);

NSLog (@ "%@", host);

NSLog (@ "received");

return NO;

}

Accept Failure

-(void) Onudpsocket: (Asyncudpsocket *) sock Didnotreceivedatawithtag: (long) tag duetoerror: (Nserror *) error{

NSLog (@ "not received ah");

}

Send failed

-(void) Onudpsocket: (Asyncudpsocket *) sock Didnotsenddatawithtag: (long) tag duetoerror: (Nserror *) error{

NSLog (@ "%@", error);

NSLog (@ "not sent Ah");

}

Start sending

-(void) Onudpsocket: (Asyncudpsocket *) sock Didsenddatawithtag: (long) tag{

NSLog (@ "send");

}

Turn off broadcast

-(void) Onudpsocketdidclose: (Asyncudpsocket *) sock{

NSLog (@ "off");

}

#pragma mark gets the current IP

-(NSString *) getipaddress {

NSString *address = @ "error";

struct Ifaddrs *interfaces = NULL;

struct Ifaddrs *temp_addr = NULL;

int success = 0;

Retrieve the current Interfaces-returns 0 on success

Success = Getifaddrs (&interfaces);

if (success = = 0) {

Loop through linked list of interfaces

TEMP_ADDR = interfaces;

while (temp_addr! = NULL) {

if (temp_addr->ifa_addr->sa_family = = af_inet) {

Check if interface is En0 which are the WiFi connection on the IPhone

if ([[[NSString Stringwithutf8string:temp_addr->ifa_name] isequaltostring:@ "En0"]) {

Get NSString from C String

address = [NSString stringwithutf8string:inet_ntoa ((struct sockaddr_in *) temp_addr->ifa_addr->sin_addr)];

}

}

TEMP_ADDR = temp_addr->ifa_next;

}

}

Free memory

Freeifaddrs (interfaces);

return address;

}

iOS Development UDP protocol send broadcast find device

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.