我想向我网络中的服务器发送请求。我做的第一件事是获取我的 IP 地址。前任。196.168.16。* . 现在我有了我的 IP 地址,我将不得不向网络中的所有 IP 发送请求,这些 IP 带有侦听端口,例如。端口 49160。所以我所做的是将请求从 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];
我想知道如何正确地迭代它。谢谢