ios開發之傳送UDP廣播並接收資料

2021-06-09 21:06:08 字數 1426 閱讀 1416

用了

asyncudpsocket, 服務端在接收到廣播後,就會做出回應,然後就可以開始通訊了。

- (void)init{

asyncudpsocket *socket=[[asyncudpsocketalloc]initwithdelegate:self];

[socketlocalport:16747];

nstimeinterval timeout=5000;

nsstring *request=@"quick_stat";

nsdata *data=[nsdatadatawithdata:[request datausingencoding:nsasciistringencoding] ];

uint16 port=8081;  

nserror *error;

[socket enablebroadcast:yeserror:&error];

[socket senddata :data tohost:@"255.255.255.255" port:port withtimeout:timeout tag:1];

[socket receivewithtimeout:-1tag:0];

nslog(@"begin scan");

- (bool)onudpsocket:(asyncudpsocket *)sock didreceivedata:(nsdata *)data withtag:(long)tag fromhost:(nsstring *)host port:(uint16)port{

nsstring* result;

result = [[nsstringalloc] initwithdata:data encoding:nsasciistringencoding];

nslog(@"%@",result);

nslog(@"received");

-(bool)onudpsocket:(asyncudpsocket *)sock didnotreceivedatawithtag:(long)tag duetoerror:(nserror *)error{

nslog(@"not received");

-(bool)onudpsocket:(asyncudpsocket *)sock didnotsenddatawithtag:(long)tag duetoerror:(nserror *)error{

nslog(@"%@",error);

nslog(@"not send");

-(bool)onudpsocket:(asyncudpsocket *)sock didsenddatawithtag:(long)tag{

nslog(@"send");

-(bool)onudpsocketdidclose:(asyncudpsocket *)sock{

nslog(@"closed");

Linux系統下UDP傳送和接收廣播訊息小例子

分類 網路通訊 2013 01 07 10 54 1336人閱讀收藏 舉報 cpp view plain copy print?傳送端 include include include include include include include include include using name...

Linux系統下UDP傳送和接收廣播訊息小例子

cpp view plain copy 傳送端 include include include include include include include include include using namespace std int main const int opt 1 設定該套接字為廣播...

udp廣播傳送端與接收端demon 超詳細注釋

udp廣播收發經典demon,超詳細注釋 1 傳送端 demon 廣播傳送端 author 樂樂 include include include include include include define port 19999 埠號 define maxdatasize 256 intmain 傳...