2012-08-17 98 views
1

因此,问题在于: 流没有在正确的时间写入,无法从我的服务器获得响应。 我知道我写正确的数据,因为当我停下来构建,将其写入服务器和服务器是幸福的。 我很困惑,为什么这不是在正确的时间发生。编程流遇到问题

我使用GCDAsyncSocket读/写插座

我做了一个单独的类来处理所有的网络, 这里是我的代码:

-(id)init{ 
    self = [super init]; 
    if (self) { 
     asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self   delegateQueue:dispatch_get_main_queue()]; 

     NSError *error = nil; 
     if (![asyncSocket connectToHost:@"localhost" onPort:8888 error:&error]) //  Asynchronous! 
     { 
      // If there was an error, it's likely something like "already connected" or  "no delegate set" 
      NSLog(@"I goofed: %@", error); 
     } 
    } 
    return self; 
} 
//GCD Async Delegate Methods 
- (void)socket:(GCDAsyncSocket *)sender didConnectToHost:(NSString *)host port:(UInt16)port 
{ 
    NSLog(@"socket:%p didConnectToHost:%@ port:%hu", sender, host, port); 
    NSLog(@"Cool, I'm connected! That was easy."); 
} 
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag 
{ 
    if (tag == 1) 
     NSLog(@"First request sent"); 
    else if (tag == 2) 
     NSLog(@"Second request sent"); 
    //read data of custom length 
    /* 
    if (tag == TAG_FIXED_LENGTH_HEADER) 
    { 
    int bodyLength = [self parseHeader:data]; 
    [socket readDataToLength:bodyLength withTimeout:-1 tag:TAG_RESPONSE_BODY]; 
    } 
    else if (tag == TAG_RESPONSE_BODY) 
    { 
    // Process the response 
    [self handleResponseBody:data]; 

    // Start reading the next response 
    [socket readDataToLength:headerLength withTimeout:-1 tag:TAG_FIXED_LENGTH_HEADER]; 
    } 
    */ 
} 

-(void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { 
    NSError *error; 
    serverJSONResponseParsed = [NSJSONSerialization JSONObjectWithData:data  options:kNilOptions error:&error]; 
} 

-(BOOL)sendString:(NSString *)sendString{ 

    NSError *error; 

    NSLog(@"Sending String: %@",sendString); 

    NSDictionary *blobData = [NSDictionary dictionaryWithObjectsAndKeys: 
           sendString,@"string", 
           nil]; 
    NSString *blobString = [[NSString alloc] 
          initWithData:[NSJSONSerialization dataWithJSONObject:blobData  options:kNilOptions error:&error] 
          encoding:NSUTF8StringEncoding]; 
    NSLog(@"Blob Created: %@", blobString); 
    NSDictionary *requestData = [NSDictionary dictionaryWithObjectsAndKeys: 
           @"send_string",@"request_type", 
            [NSNumber numberWithInt:0],@"security_level", 
            @"ios",@"device_type", 
            //No Email Provided, This is just for testing 
           blobString,@"blob", 
           nil]; 

    NSData *JSONRequestData = NULL; 
    JSONRequestData = [NSJSONSerialization dataWithJSONObject:requestData options:kNilOptions error:&error]; 
      NSLog(@"Contents of JSONRequestData: %@",[[NSString alloc]  initWithData:JSONRequestData encoding:NSUTF8StringEncoding]); 
    //Write the data 
    [asyncSocket writeData:JSONRequestData withTimeout:-1 tag:1]; 

    //read a response 
    [asyncSocket readDataToData:[GCDAsyncSocket CRLFData] withTimeout:-1 tag:2]; 

    NSDictionary *JSONResponseData = serverJSONResponseParsed; 
    /* 
    if (serverResponse != NULL) { 
     JSONResponseData = [NSJSONSerialization JSONObjectWithData:serverResponse  options:kNilOptions error:&error]; 
    } 
    */ 
    NSString *failure = [JSONResponseData objectForKey:@"failure"]; 
    if ([failure respondsToSelector:@selector(isEqualToString:)]) { 
     NSLog(@"Unknown Object, Not a NSString"); 
    } 
    if ([failure isEqualToString:@"none"]) { 
     NSLog(@"Success Sending String"); 
     return TRUE; 
    } 
    else { 
     NSLog(@"%@",failure); 
     return FALSE; 
    } 

} 

这里的控制台说什么:

2012-08-16 18:15:27.831 FlokMobile[4257:c07] Sending String: hello 
2012-08-16 18:15:27.832 FlokMobile[4257:c07] Blob Created: {"string":"hello"} 
2012-08-16 18:15:27.832 FlokMobile[4257:c07] Contents of JSONRequestData:  {"security_level":0,"request_type":"send_string","device_type":"ios","blob":"  {\"string\":\"hello\"}"} 
2012-08-16 18:15:27.833 FlokMobile[4257:c07] (null) 
2012-08-16 18:15:27.833 FlokMobile[4257:c07] It didn't work - appdel 
2012-08-16 18:15:27.850 FlokMobile[4257:c07] socket:0x6992890 didConnectToHost:127.0.0.1 port:8888 
2012-08-16 18:15:27.850 FlokMobile[4257:c07] Cool, I'm connected! That was easy. 
2012-08-16 18:15:27.851 FlokMobile[4257:c07] First request sent 

回答

5

您正在阅读本:

[asyncSocket readDataToData:[GCDAsyncSocket CRLFData] withTimeout:-1 tag:2]; 

一个希望将CRLF作为流的分离。但是我没有看到你把它附加到你的JSONRequestData中。因此,修改您的JSONRequestData是可变的,就像这样:

NSMutableData *JSONRequestData; 

然后将此行之前:

[asyncSocket writeData:JSONRequestData withTimeout:-1 tag:1]; 

补充一点:

[JSONRequestData appendData:[GCDAsyncSocket CRLFData]]; 
+0

谢谢!现在我明白了(在不知道该做什么之后有一种很棒的感觉) – khaliq 2012-08-18 08:40:34

0

当发送数据的短块后每个总是冲缓存写入要立即生效。
我没有看到你在你的代码中这样做。
如果你不这样做,那么缓冲器会被刷新,只有当它是满或流被关闭。

+1

我明白你的意思。唯一的问题是,GCDAsyncSocket自己处理它,因此没有办法“冲洗” https://groups.google.com/forum/?hl=zh-CN&fromgroups#!topic/cocoaasyncsocket/ksy7nv-a4E4%5B1-25%5D – khaliq 2012-08-17 04:03:48