Posted By

ghiboz on 08/04/11


Tagged

socket ios


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

Tintenklecks
cnathan
dvel


socket with iphone


 / Published in: Objective C
 

URL: http://www.iphonedevsdk.com/forum/iphone-sdk-development/2998-how-socket-connection.html

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:@"\n"];
  58.  
  59. resultText.font = [UIFont fontWithName:@"Helvetica" size:10.0];
  60. resultText.textColor = [UIColor whiteColor];
  61. resultText.text = [[NSString alloc] initWithFormat: @"%@\n%@", 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. }

Report this snippet  

You need to login to post a comment.