带iphone的插座

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了带iphone的插座相关的知识,希望对你有一定的参考价值。

da testare
  1. NSString *urlStr = @"192.168.178.26";
  2. if (![urlStr isEqualToString:@""])
  3. {
  4. NSURL *website = [NSURL URLWithString:urlStr];
  5. if (!website)
  6. {
  7. NSLog(@"%@ is not a valid URL");
  8. return;
  9. }
  10. NSHost *host = [NSHost hostWithName:[website host]];
  11. [NSStream getStreamsToHost:host port:3258 inputStream:&iStream outputStream:&oStream];
  12. [iStream retain];
  13. [oStream retain];
  14. [iStream setDelegate:self];
  15. [oStream setDelegate:self];
  16. [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  17. [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  18. [iStream open];
  19. [oStream open];
  20. }
  21.  
  22.  
  23. - (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
  24. {
  25. NSString *io;
  26.  
  27. if (theStream == iStream) io = @">>";
  28. else io = @"<<";
  29.  
  30. NSString *event;
  31. switch (streamEvent)
  32. {
  33. case NSStreamEventNone:
  34. event = @"NSStreamEventNone";
  35. resultText.font = [UIFont fontWithName:@"Helvetica" size:10.0];
  36. resultText.textColor = [UIColor whiteColor];
  37. resultText.text = [[NSString alloc] initWithFormat: @"Can not connect to the host!"];
  38. break;
  39. case NSStreamEventOpenCompleted:
  40. event = @"NSStreamEventOpenCompleted";
  41. break;
  42. case NSStreamEventHasBytesAvailable:
  43. event = @"NSStreamEventHasBytesAvailable";
  44. if (theStream == iStream)
  45. {
  46. uint8_t buffer[1024];
  47. int len;
  48. while ([iStream hasBytesAvailable])
  49. {
  50. len = [iStream read:buffer maxLength:sizeof(buffer)];
  51. if (len > 0)
  52. {
  53. NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
  54. NSData *theData = [[NSData alloc] initWithBytes:buffer length:len];
  55. if (nil != output)
  56. {
  57. //NSArray *arr = [output componentsSeparatedByString:@" "];
  58.  
  59. resultText.font = [UIFont fontWithName:@"Helvetica" size:10.0];
  60. resultText.textColor = [UIColor whiteColor];
  61. resultText.text = [[NSString alloc] initWithFormat: @"%@ %@", resultText.text, output];
  62. NSLog(@"%@", output);
  63. [output release];
  64. }
  65. }
  66. }
  67. }
  68. break;
  69. case NSStreamEventHasSpaceAvailable:
  70. event = @"NSStreamEventHasSpaceAvailable";
  71.  
  72. break;
  73. case NSStreamEventErrorOccurred:
  74. event = @"NSStreamEventErrorOccurred";
  75. resultText.font = [UIFont fontWithName:@"Helvetica" size:10.0];
  76. resultText.textColor = [UIColor whiteColor];
  77. resultText.text = [[NSString alloc] initWithFormat: @"Can not connect to the host!"];
  78. break;
  79. case NSStreamEventEndEncountered:
  80. event = @"NSStreamEventEndEncountered";
  81. [theStream close];
  82. [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  83. [theStream release];
  84. theStream = nil;
  85.  
  86. break;
  87. default:
  88. event = @"** Unknown";
  89. }
  90.  
  91. NSLog(@"%@ : %@", io, event);
  92. }

以上是关于带iphone的插座的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 iphone 磁力计值计算来自墙壁插座的电流电压值?

iPhone sdk 是不是支持 l2cap 蓝牙插座?

iphone / Objective c的最佳代码片段网站是啥[重复]

带插座/端口的简单锁应用程序

卡依(XLR插座和ADAT光接口的特点

301重定向没有片段[重复]