• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Having a weird error with NSStreams
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Having a weird error with NSStreams


  • Subject: Re: Having a weird error with NSStreams
  • From: Jens Alfke <email@hidden>
  • Date: Tue, 15 Sep 2009 15:52:44 -0700


On Sep 15, 2009, at 2:36 PM, Development wrote:

while (0 < remainingToWrite) {
int actuallyWritten = 0;
actuallyWritten = [_outStream write:marker maxLength:remainingToWrite];
remainingToWrite -= actuallyWritten;
marker += actuallyWritten;
}

I assume _outStream is in blocking mode (you haven't hooked it up to a runloop), right? Otherwise this is going to spin like crazy.


while ((NSInputStream*)[stream hasBytesAvailable])
{
NSLog(@"In comming");
if (![(NSInputStream*)stream getBuffer:&buffer length:&len]) {
int amount = [(NSInputStream*)stream read:buf maxLength:sizeof(buf)];
buffer = buf;
len = amount;
}
if (0 < len) {
if (!dataBuffer) {
dataBuffer = [[NSMutableData alloc] init];
}
[dataBuffer appendBytes:buffer length:len];
[cardData appendData:dataBuffer];
}
}

This stuff with dataBuffer doesn't make sense to me. Every time around the loop you append the incoming bytes to dataBuffer, but then you append all of dataBuffer to cardData -- so cardData will contain multiple copies of the incoming bytes. So if it reads "111", then "222", then "333", cardData will end up with "111111222111222333".


NSString * string = [[NSString alloc]initWithCString: [dataBuffer bytes]];

Yikes! This has a couple of major problems.
* It assumes that dataBuffer is null-terminated. There's nothing in your code that guarantees this. If for some reason dataBuffer's data doesn't end with a null bytes, your app could crash.
* Never use -initWithCString:. It's deprecated, for good reason. The encoding is unspecified, so it ends up using whatever your "default encoding" is, and that varies depending on the user's preferred language. The end result is that the contents of the string are quite unpredictable if the data contains any non-ascii characters. Instead you should use +initWithCString:encoding:.
In this case the method you want is probably -[initWithData:encoding:].


<plug href="http://bitbucket.org/snej/mynetwork/wiki/Home";>
Have you looked at my MYNetwork library? It does all the socket gunk for you, plus Bonjour, and it has a very powerful high-level protocol called BLIP for sending messages and replies over a socket.
</plug>


—Jens_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Having a weird error with NSStreams (From: Development <email@hidden>)

  • Prev by Date: Re: Custom Fonts in UI Webview
  • Next by Date: Re: Having a weird error with NSStreams
  • Previous by thread: Having a weird error with NSStreams
  • Next by thread: Re: Having a weird error with NSStreams
  • Index(es):
    • Date
    • Thread