views:

437

answers:

2

I'm working with sockets and trying to simply write a string to the socket. I found a couple of examples on how to do this, such as the following example (oStream is an NSOutputStream). This is all within the NSStreamEventHasSpaceAvailable.

uint8_t buffer[11] = "I send this";             
int len;
len = [oStream write:buffer maxLength:sizeof(buffer)];

That works great. So I tried modifying it to make it customizable by getting the bytes of out an NSString.

int len;
NSString* strId;
strId = @"string will be customized with several lines of code here";

uint8_t buffer[[strId lengthOfBytesUsingEncoding:NSASCIIStringEncoding]];                       
[strId getBytes:buffer maxLength:sizeof(buffer) usedLength:NULL encoding:NSASCIIStringEncoding options:NULL range:NSRangeFromString(strId) remainingRange:NULL];
len = [oStream write:buffer maxLength:sizeof(buffer)];

This isn't working at all. It writes complete gibberish into the buffer (probably the bytes) and I'm getting a warning for the getBytes line of "passing argument 5 of getBytes makes integer from pointer without a cast."

Sorry if this has been asked before, couldn't find an answer from searching and it looked like the problem should be very simple to fix. Thanks!

EDIT: I've found something that seems to work, but I'm not sure if it's proper programming of something like this. Please see the answer below. Is that dangerous code to be using in this situation?

+1  A: 

What about -[NSString cStringUsingEncoding:]?

zneak
Not sure where I'd put that, but I tried this:`uint8_t buffer[[strId lengthOfBytesUsingEncoding:NSASCIIStringEncoding]] = [strId cStringUsingEncoding:NSASCIIStringEncoding];`Which gave a "Variable-sized object may not be initialized".And this: `uint8_t buffer[[strId lengthOfBytesUsingEncoding:NSASCIIStringEncoding]];` `buffer = [strId cStringUsingEncoding:NSASCIIStringEncoding];`Which gave "Incompatible types in assignment."I also tried typecasting the second one as a `uint8_t` but that didn't work either. I don't fully understand this area so I'm just taking shots in the dark.
mjdth
It's because you can't set a dynamic array size in C, and Objective-C, besides its specific rules, works under the C rules. That being said, cStringUsingEncoding does not require you to create an array. It returns a `const char*`, so all you need to declare is a `const char*` to receive the result: `const char* buffer = [strId cStringUsingEncoding:NSASCIIStringEncoding];`You may then use it as an array (except you can't use `sizeof` on it).
zneak
+1  A: 

After more research I've found that this seems to work:

len = [oStream write:[[strId dataUsingEncoding:NSASCIIStringEncoding] bytes] maxLength:[strId lengthOfBytesUsingEncoding:NSASCIIStringEncoding]];

I feel as if that's not very safe since it's using a couple of different ways of getting the data into the NSOutputStream as well as the length. Is there going to a a problem doing it like this?

mjdth
This was the method I ended up using but I'm going to mark the other as the correct answer because it works as well.
mjdth