0

我必须创建一个使用套接字编程的聊天应用程序。 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);
 }

我已经尝试了很多并用谷歌搜索,但我没有找到任何解决方案。
它正在消磨我的时间,所以如果有人对此进行了研究,请指导我并发布示例代码。

4

0 回答 0