我想将请求发送到网络中的服务器。我做的第一件事就是获取我的IP地址。恩。 196.168.16。 * 。现在我有了我的IP地址,我将不得不向具有侦听端口的那些网络中的所有IP发送请求,例如。所以我做的是将请求从192.168.16.1发送到192.168.16.255,如果IP有一个49160的监听端口,他们将收到我的请求。然后一个提示将出现在PC上...通过我的服务器是PC的方式..然后PC用户将接受它,然后我将收到他们的返回消息。我能够以静态的方式发送请求。但是当我尝试循环使用它的方法时,最好的工作。
以下是示例代码:
static IOStreamHelper *_sharedInstance = nil;
+ (IOStreamHelper*)sharedInstance
{
if (_sharedInstance == nil) {
_sharedInstance = [[IOStreamHelper alloc] init];
}
return _sharedInstance;
}
-(void)initWithIpAddress:(CFStringRef)ipAddr port:(int)port
{
NSString*str = (__bridge_transfer NSString*)ipAddr;
NSLog(@"initWithIP:%@:%i",str,port);
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)ipAddr, port, &readStream, &writeStream);
inputStream = (__bridge_transfer NSInputStream *)readStream;
outputStream = (__bridge_transfer NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
}
-(void)sendStringRequest
{
NSString*space = [NSString stringWithFormat:@"%@",[NSByteCountFormatter stringFromByteCount:[self getFreeDiskspace] countStyle:NSByteCountFormatterCountStyleFile]];
NSString *message = [NSString stringWithFormat:@"fishtune_c;%@(%@),1092809312741,jyce09@gmail.com,%@;",[self platformString],[[GetIpAndMacAddress sharedInstance] getMacAddress],space];
messageData = [[NSData alloc] initWithData:[message dataUsingEncoding:NSUTF8StringEncoding]];
[outputStream write:[messageData bytes] maxLength:[messageData length]];
}
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
{
NSLog(@"stream event %i", streamEvent);
switch (streamEvent) {
case NSStreamEventOpenCompleted:
NSLog(@"Stream opened");
break;
case NSStreamEventHasBytesAvailable:
if (theStream == inputStream) {
uint8_t buffer[1024];//1024
int len;
while ([inputStream hasBytesAvailable]) {
len = [inputStream read:buffer maxLength:sizeof(buffer)];
if (len > 0) {
NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSUTF8StringEncoding];
if (nil != output) {
NSLog(@"server reply: %@", output);
}
}
}
}
break;
case NSStreamEventHasSpaceAvailable:
NSLog(@"Stream has space available now");
[self sendStringRequest];
break;
case NSStreamEventErrorOccurred:
{
NSLog(@"Can not connect to the host!");
NSError *theError = [theStream streamError];
NSLog(@"%@",[NSString stringWithFormat:@"%@",[theError localizedDescription]]);
}
break;
case NSStreamEventEndEncountered:
[theStream close];
[theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
theStream = nil;
break;
default:
NSLog(@"Unknown event");
}
}
然后我调用这个方法。这是我想要从1迭代到255的方法:
CFStringRef aCFString = (__bridge CFStringRef)@"IPfrom 1 to 255";
[[IOStreamHelper sharedInstance] initWithIpAddress:aCFString port:49160];
我想知道如何正确迭代这个。感谢