Having trouble programming streams

后端 未结 2 1587
攒了一身酷
攒了一身酷 2020-12-18 16:03

So here\'s the issue: The stream isn\'t writing at the right time and is unable to get a response from my server. I know I\'m writing the right data, because when I stop th

相关标签:
2条回答
  • 2020-12-18 17:03

    You are reading with this:      

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

    Which expects a CRLF as a separator of the stream. But I don't see where you append it to your JSONRequestData. So modifying your JSONRequestData to be mutable, like so:

    NSMutableData *JSONRequestData;
    

    And then before this line:

     

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

    Add this:

    [JSONRequestData appendData:[GCDAsyncSocket CRLFData]];
    
    0 讨论(0)
  • 2020-12-18 17:06

    When sending short chunks of data always flush the buffer after each write which you want to have an immediate effect.
    I don't see you doing that in your code.
    If you don't do it then the buffer will be flushed only when it is full or the stream is closed.

    0 讨论(0)
提交回复
热议问题