Get a char from NSInputStream/NSMutableData

Discussion in 'Mac Programming' started by jimgeldermann, Sep 10, 2010.

  1. jimgeldermann macrumors newbie

    Sep 10, 2010
    Not surprisingly I'm a bit confused. I'm using AsyncSocket to manage my connection and using their echo server to bounce messages.

    When I place a char on the network:
    char i = 0;
    i =  64;
    [dataToSend appendData:[NSMutableData dataWithBytes:&i length:sizeof(char)]];
    When I decompose the message from the send function I get 64 as the value

    NSData *tstData = [NSData dataWithBytes:&i length:sizeof(char)];
    	unsigned char byteBuffer[sizeof(char)];
    	[tstData getBytes:byteBuffer];
    	NSLog(@"Output: %d", byteBuffer[0]);
    But when I receive the message and move the input stream to NSMutableData and decompose it (byteBuffer is the stream as NSMutableData)

    NSData *tstData = [NSData dataWithBytes:[byteBuffer subdataWithRange:NSMakeRange(0, [byteBuffer length])] length:[byteBuffer length]];
    	unsigned char myBytes[[tstData length]];
    	[tstData getBytes:myBytes];
    	for(int i = 0; i <[tstData length]; i++){
    		NSLog( @"\n\t pos: %d myBytes size %d value %d",i, [tstData length], myBytes[i]);
    I always get 104 as the first byte, where I am expecting the value of i from the send function.

  2. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    Post compilable code. It should be complete yet minimal. The purpose is for someone else to replicate the problem, and analyze it with minimal extraneous code.

    Don't post the EchoServer, but do post the URL where you obtained it.

Share This Page