我无法向套接字服务器发送消息

时间:2013-08-06 10:47:41

标签: iphone ios objective-c sockets ios6

我必须使用套接字编程iOS创建聊天应用

我无法接收和发送数据,没有错误或崩溃。

我的代码:

-(IBAction)sendButtonAction:(id)sender
{
    NSString *ipAddress = @"my server ip address";
    NSString *portNo = @"my port number";

    if(inputStream && outputStream)
       [self close];

    NSURL *website = [NSURL URLWithString:ipAddress];

    if (!website)
    {
        NSLog(@"%@ is not a valid URL", website);
    }
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef) [website host], [portNo intValue], &readStream, &writeStream);

    self.inputStream = (__bridge_transfer NSInputStream *)readStream;
    self.outputStream = (__bridge_transfer NSOutputStream *)writeStream;

    NSLog(@"inputStream ===%@",inputStream);
    NSLog(@"outputStream ===%@",outputStream);

    [self open];
}

- (void)open
{
    [self.inputStream setDelegate:self];
    [self.outputStream setDelegate:self];
    [self.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                            forMode:NSDefaultRunLoopMode];
    [self.outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                             forMode:NSDefaultRunLoopMode];
    [self.inputStream open];
    [self.outputStream open];

    NSLog(@"Socket Init: %@",@"");
}

- (void)dataSending:(NSString*)data
{
    NSLog(@"data ===%@",data);

    if(outputStream)
    {
        if(![outputStream hasSpaceAvailable])
            return;
        NSString *response  = [NSString stringWithFormat:@"%@<EOF>",data];

        NSLog(@"response ===--------------->%@",response);

        NSData *msgdata = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];

        NSLog(@"msgdata ===--------------->%@",msgdata);
        [outputStream write:[msgdata bytes] maxLength:[msgdata length]];

        NSLog(@"Sent data----------------------%@",data);
       }
}

-(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
{
     NSLog(@"stream event %i", streamEvent);

    NSString *event;
    switch (streamEvent)
    {
        case NSStreamEventNone:
            event = @"NSStreamEventNone";
            break;
        case NSStreamEventOpenCompleted:
            event = @"NSStreamEventOpenCompleted";
            break;
        case NSStreamEventHasBytesAvailable:
            event = @"NSStreamEventHasBytesAvailable";
            if (theStream == inputStream)
            {
                uint8_t buffer[1024];
                int len;
                while ([inputStream hasBytesAvailable])
                {
                    len = [inputStream read:buffer maxLength:1024];
                    if (len > 0)
                    {
                        NSMutableString *output = [[NSMutableString alloc]
                                               initWithBytes:buffer length:len encoding:NSUTF8StringEncoding];
                        NSLog(@"Received data--------------------%@", output);
                    }
                }
            }
            break;
        case NSStreamEventHasSpaceAvailable:
            event = @"NSStreamEventHasSpaceAvailable";
            [self dataSending:self.messageTextFiled.text];
            break;
        case NSStreamEventErrorOccurred:
            event = @"NSStreamEventErrorOccurred"; 
            [self close];
            break;
        case NSStreamEventEndEncountered:
            event = @"NSStreamEventEndEncountered";
            [theStream close];
            [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
            [self close];
            break;
        default:
            event = @"Unknown";
            break;
     }
      NSLog(@"event------%@",event);
 }

我已经尝试了很多并用Google搜索,但我找不到任何解决方案  这是在浪费我的时间,所以如果有人参与其中,请指导我并发布示例代码。

0 个答案:

没有答案