3 Replies
      Latest reply: Jan 10, 2017 1:04 PM by geef RSS
      geef Level 1 Level 1 (0 points)

        I am trying to build a communicator ios client, I have a problem utilizing the socket connection using Foundation networking APIs; created socket is able to receive data fine however when attempting to send something, data is not send until ios application is terminated, at which point all earlier data is received by server. I am new to objective c, but tried to follow several tutorials and always encounter same issue.

         

        I attach my code below:

        - (void)viewDidLoad {
            [super viewDidLoad];
            [self openSocket];
        }
        - (void)openSocket {
            CFReadStreamRef readStream;
            CFWriteStreamRef writeStream;
            CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (CFStringRef)@"localhost", 1235, &readStream, &writeStream);
           
            inputStream = (NSInputStream *)CFBridgingRelease(readStream);
            [inputStream setDelegate:self];
            [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
            [inputStream open];
           
            outputStream = (NSOutputStream *)CFBridgingRelease(writeStream);
            [outputStream setDelegate:self];
            [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
            [outputStream open];
        }
        - (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
            NSLog(@"stream event %lu", (unsigned long)streamEvent);
        }
        - (IBAction)sendMsg:(id)sender {
            NSString *response  = [NSString stringWithFormat:@"my test msg"];
            NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
            [outputStream write:[data bytes] maxLength:[data length]];
        }
        
        • Re: Tcp Socket Data Sending Issue
          NotMyName Level 4 Level 4 (550 points)

          You've asked your question in a vague and unspecific manner such that anyone who wanted to help you would have to guess what particular errors or mistakes you've encountered.

           

          Post your code if you want someone to explain what you've done wrong.

          • Re: Tcp Socket Data Sending Issue
            eskimo Apple Staff Apple Staff (6,055 points)

            … when attempting to send something, data is not send until ios application is terminated, at which point all earlier data is received by server.

            CFSocketStream (which is what you get when you create a stream in this way) does not buffer any data in user space.  The data you write gets immediately placed in the kernel socket buffer and, from there, should go out on the ‘wire’.  I’ve worked with developer who’ve had problems like this before and they almost always end up being caused by buffering on the server.

            You can determine whether this is the case by using a packet trace to see whether the data has hit the wire or not.  See QA1176 Getting a Packet Trace for details.

            Share and Enjoy

            Quinn “The Eskimo!”
            Apple Developer Relations, Developer Technical Support, Core OS/Hardware
            let myEmail = "eskimo" + "1" + "@apple.com"