Nikh1414
Nikh1414

Reputation: 1256

iOS: Socket Connection Error in sending buffer

We have tried too many diffrent approaches to solve our issue and also searched over net but we only found example of sending string and not the byte array. We require to send byte array through socket connection. Please read issue explanation below.

We require to connect wi-fi device with iOS app. We have successfully connected the device but when we send the command in byte array format, It is returning NSStreamEventHasSpaceAvailable in response. Which is wrong, We require NSStreamEventHasBytesAvailable event.

Below is the code for connection:

-(void) initNetworkCommunication:(NSString*)strHostName {

CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)strHostName, 2000, &readStream, &writeStream);

inputStream = (NSInputStream *)readStream;
outputStream = (NSOutputStream *)writeStream;



[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];

}

When connection is open, we are calling below method to send the command:

 - (IBAction) sendCommand {
 unsigned char *buffer[5] = {0x3a,0x04,0x01,0x07,0x0c};

NSData *data = [NSData dataWithBytes:buffer length:5];
[outputStream write:[data bytes] maxLength:5];

}

So there is some issue in sendCommand method because we are receiving NSStreamEventHasSpaceAvailable and that is wrong. Because it should be returned NSStreamEventHasBytesAvailable in the response. Can any one please help us how to send byte array {0x3a,0x04,0x01,0x07,0x0c} in iOS so we can receive NSStreamEventHasBytesAvailable event.

According to command manual, When device receives command in correct format then it will return acknowledgement. Below is the manual description.

All commands are 16 bits and are placed in the Data byte 1(MSB) and Data byte 2(LSB). The response to a command can either be a specific response relating to the command or a simple response. The three simple responses are ACK, NAK and UNK. An ACK signifies that the command was received successfully. A NAK indicates that there was an error with either the length byte or an incorrect checksum was calculated. An UNK response indicates that the recipient does not recognize the command that was sent.

So we should receive any of the above flag(ACK OR NAK OR UNK) but we are receiving NSStreamEventHasSpaceAvailable and this is wrong. Any one please help me to solve my problem.

Thanks in advance.

Upvotes: 2

Views: 579

Answers (1)

Paulw11
Paulw11

Reputation: 114773

NSStreamEventHasSpaceAvailable is the correct event to receive after sending data on an output stream - it indicates that you can write at least one byte without blocking (ie. There is space to write data available). NSstreamEventHasBytesAvailable will be signalled against an input stream when data is received on the socket associated with that input stream to indicate that you can issue a read without blocking.

If the device you are sending the data to responds to that data then I would expect you to receive NSStreamEventHasBytesAvailable on inputStream

Upvotes: 2

Related Questions