Yuy*_*tsu 20 sockets objective-c ios gcdasyncsocket gcdasyncudpsocket
我正在制作一个应用程序来发送UDP数据包,以便打开一个LED bulb.当我连接到由Ad-hoc创建的创建时,我已经能够执行所有操作Wifi bridge.
现在,我想配置Wifi bridge它,以便它可以连接到我的主路由器.我有AT命令设置来执行此过程,但不知何故,我无法收到Wifi bridge我发送给它的命令的响应.
程序如下: -
步骤1:将UDP消息发送到LAN广播IP地址"10.10.100.255"和端口48899 =>"Link_Wi-Fi"
LAN上的所有Wifi网桥都将响应其详细信息.回复是"10.10.100.254,ACCF232483E8"
步骤2 :(可选择更改wifi网桥上的设置):然后将"+ ok"发送到LimitlessLED Wifi Bridge.将UDP消息发送到从步骤1"10.10.100.254"=>"+ ok"返回的响应IP地址
发送UDP包的代码如下
-(void)configureWifi{
counter++;
NSString *host = @"10.10.100.255";
if ([host length] == 0)
{
[self logError:@"Address required"];
return;
}
int port = 48899; //[portField.text intValue];
if (port <= 0 || port > 65535)
{
[self logError:@"Valid port required"];
return;
}
NSString *msg = @"Link_Wi-Fi";
NSData *data = [msg dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"the message sent is %@", data);
[udpSocket sendData:data toHost:host port:port withTimeout:-1 tag:tag];
}
Run Code Online (Sandbox Code Playgroud)
现在,为了设置套接字并接收数据,我正在使用这两个委托方法:
- (void)setupSocket
{
// Setup our socket.
// The socket will invoke our delegate methods using the usual delegate paradigm.
// However, it will invoke the delegate methods on a specified GCD delegate dispatch queue.
//
// Now we can configure the delegate dispatch queues however we want.
// We could simply use the main dispatc queue, so the delegate methods are invoked on the main thread.
// Or we could use a dedicated dispatch queue, which could be helpful if we were doing a lot of processing.
//
// The best approach for your application will depend upon convenience, requirements and performance.
//
// For this simple example, we're just going to use the main thread.
udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError *error = nil;
if (![udpSocket bindToPort:0 error:&error])
{
[self logError:FORMAT(@"Error binding: %@", error)];
return;
}
if (![udpSocket beginReceiving:&error])
{
[self logError:FORMAT(@"Error receiving: %@", error)];
return;
}
[self logInfo:@"Ready"];
}
Run Code Online (Sandbox Code Playgroud)
并且为了接收数据,这是在发送UDP分组之后被调用的方法.这是GCDAsyncUdpSocket我在项目中用于发送和接收UDP数据包的类的委托方法.
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
fromAddress:(NSData *)address
withFilterContext:(id)filterContext
{
NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (msg)
{
[self logMessage:FORMAT(@"RECV: %@", msg)];
}
else
{
NSString *host = nil;
uint16_t port = 0;
[GCDAsyncUdpSocket getHost:&host port:&port fromAddress:address];
[self logInfo:FORMAT(@"RECV: Unknown message from: %@:%hu", host, port)];
}
}
Run Code Online (Sandbox Code Playgroud)
一旦我能够收到响应,我将能够发送下一个AT命令以配置桥接器.
谢谢.任何帮助将不胜感激.
以下是我建议您使用的故障排除步骤:
1-我假设您正在使用ARC,因此请确保您的udpSocket变量在整个异步通信过程中具有强引用。如果它被释放,那么这可以解释为什么没有回调。
2- 确保沟通确实按照您想象的方式进行。Wireshark使用诸如捕获网络上交换的数据包之类的软件。这应该可以让您确认您的数据包确实在呼叫时发送sendData:,并且还可以让您确认是否收到回复。
3- 确保您GCDAsyncUdpSocket正确使用。考虑到您想要广播消息,您不应该bindToPort:error:在您的setupSocket方法中调用。相反,你应该打电话给enableBroadcast:error:. 考虑到您还想在广播后接收数据包,您应该使用该connectToHost:onPort:error:方法来更改套接字的状态以允许双向通信。完成后,您可以将您的使用替换sendData:toHost:port:withTimeout:tag:为sendData:withTimeout:tag:. 最后,您可以调用,beginReceiving:以便为任何传入数据包调用委托。
4-如果这仍然不能让您完成它,我建议您通读 GCDAsyncUdpSocket 的文档,该文档有很好的文档记录。
| 归档时间: |
|
| 查看次数: |
2161 次 |
| 最近记录: |