2

I want to send small packages using the NSOutputStream in Objective-C.

I have the same problem this user has: Cocoa NSOutputStream send to a connection

An answer is to just add "\n" to the end of the string. I tried so and it does not work.

Does someone of you see my problem there? Might the encoding be the problem?

// Append the line break
theMessage = [theMessage stringByAppendingString:@"\n"];

// Create the data object
NSData *d = [theMessage dataUsingEncoding:NSASCIIStringEncoding];

// Send the message
[self.outputStream write:[d bytes] maxLength:[d length]]

Edit: I get the outputstream by calling this method from a NSNetservice-Object:

CFStreamCreatePairWithSocketToHost(NULL, url, port, &inputStream, &outputStream);

Edit2: The problem is that if I send a lot of messages very fast, the server recieves the message as one string, not as two seperated

Community
  • 1
  • 1
0x6368
  • 337
  • 2
  • 17
  • So what is outputStream initialized as? – Richard J. Ross III Apr 30 '12 at 19:18
  • I got it calling the method above – 0x6368 Apr 30 '12 at 20:09
  • What do you mean by "it doesn't work"? You don't give us what your output is or what errors occur or anything. Are you sure that `theMessage` can be encoded in ASCII? If it contains any characters with accents (e.g. `ç`, `é`, etc.) then it won't work when converted to ASCII. – dreamlax Apr 30 '12 at 22:53
  • Thanks for your comment, I added the exact problem to the thread. The message can be encoded with ASCII, but I also tried UTF-8, UTF-16, etc.; it does not help – 0x6368 May 01 '12 at 05:57

0 Answers0