2012-03-15 65 views
1

我會從iPhone發送UDP廣播,然後從打開了此端口的所有設備偵聽超時時間段的UDP響應。我的來自同一子網的自定義設備是否會回答? (如果我的是192.168.1.100並且自定義設備的IP是192.168.1.201) 如何使用「SmallSockets」或「cocoaAsyncSocket」? 使用什麼函數來偵聽響應? Thanx!將UDP請求發送到255.255.255.255並等待iOS中的回覆

回答

3

我決定使用cocoaAsyncSocket。 廣播,你可以使用:

[udpSocket sendData:datatosend toHost:@"192.168.1.113" port:port withTimeout:-1 tag:0]; 

接收:

- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext 
{ 
    NSString *msg = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]; 

    NSString *host = nil; 
    uint16_t port = 0; 
    [GCDAsyncUdpSocket getHost:&host port:&port fromAddress:address]; 

    if (msg) 
    { 
     NSLog(@"Message = %@, Adress = %@ %i",msg,host,port); 
    } 
    else 
    { 
     NSLog(@"Error converting received data into UTF-8 String"); 
    } 
} 
相關問題