johnrocs
johnrocs

Reputation: 41

iPhone Socket Stream Behavior - Sending bytes to a Server.. NSStream/CFStream

Hello all and thanks for looking,

Explanation of Environment

I'm working on an app that communicates with an arduino (server, for all practical purposes) that is listening on port 2000. Connecting to the arduino is done via an ad-hoc network, so the IP address of the "server" is 169.254.1.1. Upon connecting, a client will receive an IP in the 169.254.0.0 subnet.

My Current Code

After a bit of tinkering (this is my first go at Objective C), I've managed to open a socket and establish a read/write stream between the iPhone and Arduino with the following code...

In (void)viewDidLoad I call [self initNetworkCommunication];

And define that as follows...

-(void) initNetworkCommunication 
{
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;

    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"169.254.1.1", 2000, &readStream, &writeStream);


    inputStream = (__bridge NSInputStream *)readStream;
    outputStream = (__bridge NSOutputStream *)writeStream;
    [inputStream setDelegate:self];
    [outputStream setDelegate:self];
    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [inputStream open];
    [outputStream open];
}

I have created this function to send the defined byte, however I'm not sure if I am doing this correctly. An explanation of sending bytes in hex/binary format would be super helpful as I haven't found a good tutorial/code snippet in my research. This works on a timer (for holding down a button) which works fine. I am specifically wondering if I am sending the byte correctly.

- (void)sendByte:(NSTimer *)timer
{
    Byte allDown = 0x00F0;
    NSMutableData *sendData = [NSMutableData dataWithLength:sizeof(allDown)];
    [sendData appendBytes:&allDown length:sizeof(allDown)];
    [outputStream write:[sendData bytes] maxLength:[sendData length]];
}

My Problem / Question(s)

Upon building/testing this app on my iPhone, I connected to the Arduino and pressed the button attached to the sendByte function and saw my indicator LED's (on the arduino) light up like a christmas tree. The only way those LED's turn on is if the arduino is receiving certain bytes on port 2000. This is strange, because I thought that I was only sending one specific byte 0x00F0. In the above code, is there any reason that bytes other than 0x00F0 would be sent? i.e. does opening up the socket and read/write streams send any bytes?

Furthermore, an example of sending binary data (i.e. 0001 or 10000000) or hex data (i.e. 0F) to socket/stream would be greatly appreciated. If I'm doing this completely wrong, or if there is a more simple method of sending bytes to a server, I'd be interested in learning that as well!

If any additional information is required, please ask and I will do my best to provide.

Thank you for taking the time to read this, I'm looking forward to any responses!

--John

Upvotes: 4

Views: 2514

Answers (1)

al45tair
al45tair

Reputation: 4433

The problem here is that you’re creating an NSMutableData with length 1 (which will initially contain the byte 0x00), then appending 0xf0 to it. That is, your NSMutableData is actually of length 2 and contains 0x00 0xf0.

Additionally, the NSMutableData appears to serve no useful purpose here, other than complicating your code. You probably want something more like:

- (void)sendByteF0:(NSTimer *)timer
{
    uint8_t byte = 0xf0;
    [outputStream write:&byte maxLength:1];
}

You might also consider paying attention to the result of the -write:maxLength method, since it can fail — and in that case it might return either 0 or -1, depending on exactly what happened.

Upvotes: 1

Related Questions