我完全是网络新手,但是我是一名 c/c++ 程序员,并且正在使用 Objective-c(这是针对 OSX/iPhone).
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).
我正在尝试学习如何使用 cfsocket 发送带有 UDP 套接字的魔术包.我已经看到有 AsyncUDPSocket 库之类的库,但是我不想使用这些库.
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.
我试图查看苹果的 UDPecho 文件,但作为初学者,它确实让我感到困惑.我用谷歌搜索了很多,我把下面的代码放在一起,我有一个数据包嗅探器正在运行,它没有注册任何正在发送的东西.我知道我的代码缺少很多错误捕获,但我只是想先了解基础知识.
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.
这段代码对吗?(抱歉,如果这个接缝模棱两可)我的意思是我错过了代码的某些阶段,例如:CFSocketSetAddress?
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.
谢谢
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; } 推荐答案你的问题很简单:你没有告诉CFSocket你希望它把数据发送到哪里.
Your problem is very simple: you are not telling the CFSocket where you want it to send the data.
你在 addr 中小心地构造了一个地址,但随后不要使用它.您要么需要调用 CFSocketSetAddress 来设置套接字上的目标地址(如果您希望发送的所有数据包都发送到同一目标),或者将地址作为第二个参数传递给 CFSocketSendData.
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 发送数据包?
发布评论