dispatch_queue_t udpSocketQueue = dispatch_queue_create("com.manmanlai.updSocketQueue", DISPATCH_QUEUE_CONCURRENT);
self.udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:udpSocketQueue];
- (void)startUdpSocket
{
NSError *error = nil;
if (![self.udpSocket bindToPort:UDP_PORT error:&error])
{
NSLog(@"Error starting server (bind): %@", error);
return;
}
if (![self.udpSocket beginReceiving:&error])
{
[self.udpSocket close];
NSLog(@"Error starting server (recv): %@", error);
return;
}
NSLog(@"Udp Echo server started on port %hu", [self.udpSocket localPort]);
}
- (void)stopUdpSocket
{
[self.udpSocket close];
}
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
fromAddress:(NSData *)address
withFilterContext:(id)filterContext
- (void)sendData:(NSData *)data toAddress:(NSData *)remoteAddr withTimeout:(NSTimeInterval)timeout tag:(long)tag
dispatch_queue_t tcpSocketQueue = dispatch_queue_create("com.manmanlai.tcpSocketQueue", DISPATCH_QUEUE_CONCURRENT);
GCDAsyncSocket *tcpSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:tcpSocketQueue];
-(BOOL)connectToHost:(NSString*)host onPort:(uint16_t)port error:(NSError **)errPtr
NSError *error = nil;
if (![tcpSocket connectToHost:device.address
onPort:5555
error:&error])
{
NSLog(@"Error connecting: %@", error);
} else {
NSLog(@“Connected");
}
-(void)writeData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag
- (void)readDataWithTimeout:(NSTimeInterval)timeout tag:(long)tag
// TCP socket已连接
(void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
// TCP socket已断开
(void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
// TCP socket已写入数据
(void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
// TCP socket已发送数据
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
【iOS与EV3混合机器人编程系列之四】iOS_WiFi_EV3_Library 剖析之一:WiFi UDP和TCP,布布扣,bubuko.com
【iOS与EV3混合机器人编程系列之四】iOS_WiFi_EV3_Library 剖析之一:WiFi UDP和TCP
原文:http://blog.csdn.net/songrotek/article/details/37832493