如何通过UDP CFSocket发送数据包?

编程入门 行业动态 更新时间:2024-10-24 09:28:28
本文介绍了如何通过UDP CFSocket发送数据包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是一个完整的新手到网络,但我是一个C / C ++程序员,在Objective-C我的工作(这是OSX / iPhone)。

我想学习如何发送魔术包用cfsocket UDP套接字。我见过有库,比如AsyncUDPSocket库,但是我不想用这些。

我试图来看看苹果UDPecho文件,但作为一个初学者它确实让我困惑。我GOOGLE了周围了很多,我有放在一起code以下,我有一个数据包嗅探器的运行,并没有任何注册被发送。我明白我的code丢失的错误捕获了很多,但我只是试图让基础知识第一。

这是code吧? (道歉,如果这接缝暧昧),我的意思是我错过了code的某些阶段,如:CFSocketSetAddress

如果任何人知道任何真正好的教程,这将有助于。

任何帮助是极大AP preciated,正如我在这是一个完整的初学者。

感谢

INT主(INT ARGC,为const char * argv的[]){    NSAutoreleasePool *池= [[NSAutoreleasePool的alloc]初始化];    CFSocketRef WOLsocket;    WOLsocket = CFSocketCreate(kCFAllocatorDefault,PF_INET,SOCK_DGRAM,IPPROTO_UDP,0,NULL,NULL);    如果(插座== NULL){        的NSLog(@CfSocketCreate失败);    }其他{        如果(插座){            的NSLog(@插座创造:));                结构sockaddr_in的地址;                memset的(放,地址,0,sizeof的(地址));                addr.sin_len = sizeof的(地址);                addr.sin_family = AF_INET;                addr.sin_port = htons(9); //港口                INET_ATON(255.255.255.255,&放大器; addr.sin_addr); // IP地址                CHAR ethadd [] =HelloWorld的;                CFDataRef数据= CFDataCreate(NULL,(常量UINT8 *)ethadd,sizeof的(ethadd));                CFSocketSendData(插座,NULL,数据,0);}    }    [池排水]    返回0;}

解决方案

您的问题很简单:你不告诉你想让它发送数据CFSocket

您精心构建的地址地址,但不使用它。你要么需要调用 CFSocketSetAddress 来的目的地址在插座上(如果你希望所有的数据包发送给去同一个目的地),或通过地址作为第二参数 CFSocketSendData 。

I am a complete newbie to networking, however I am a c/c++ programmer, and am working in objective-c (This is for OSX/iPhone).

I am trying to learn how to send a magic packet with a UDP socket using cfsocket. I've seen there are libraries such as AsyncUDPSocket library, however I do not want to use these.

I attempted to look at apples UDPecho file but, as a beginner it did confuse me. I've googled around ALOT, and I have put together the code below, I have a packet sniffer running and it doesn't register anything being sent. I understand my code is missing alot of error catching, however I am just trying to get the basics in first.

Is this code right? (apologies if this seams ambiguous) What I mean is am I missing certain stages of the code such as: CFSocketSetAddress?

If anyone knows any really good tutorials it would help.

Any help is greatly appreciated, as I am a complete beginner at this.

Thanks

int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; CFSocketRef WOLsocket; WOLsocket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_DGRAM, IPPROTO_UDP, 0, NULL, NULL); if ( socket == NULL) { NSLog(@"CfSocketCreate Failed"); }else{ if( socket ) { NSLog(@"Socket created :)"); struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_len = sizeof(addr); addr.sin_family = AF_INET; addr.sin_port = htons(9); //port inet_aton("255.255.255.255", &addr.sin_addr); //ip adress char ethadd []= "helloworld"; CFDataRef Data = CFDataCreate(NULL, (const UInt8*)ethadd, sizeof(ethadd)); CFSocketSendData(socket,NULL, Data, 0);} } [pool drain]; return 0; }

解决方案

Your problem is very simple: you are not telling the CFSocket where you want it to send the data.

You carefully construct an address in addr, but then don't use it. You either need to call CFSocketSetAddress to set the destination address on the socket (if you want all packets you send to go to the same destination), or pass the address as the 2nd argument to CFSocketSendData.

更多推荐

如何通过UDP CFSocket发送数据包?

本文发布于:2023-08-07 10:35:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1318647.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据包   UDP   CFSocket

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!