0

I've written a server with Python (Twisted) and now want to connect it with iOS, but having some trouble.

This is how I connect to the server:

CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"localhost", 3000, &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];

The problem comes with the RunLoop. Imagine that I wan't to send to the server one message. Then I would do:

NSData *data = [[NSData alloc] initWithData:[message dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];

BUT what if I wan't to send TWO messages, one after the other? Then I could run the code with message1 and message2... but then what the client really sends is a concatenation of message1+message2, not two different messages. I guess this is because I write the messages to the outputStream in the same "loop", so when the stream finally decides to send the data, it sends both... I can't figure out any solution. The same happens if SERVER sends more than one message to CLIENT "very fast". What should I do?

4

1 回答 1

0

定义“协议”——基本上是定义参与者理解的“语言”的“语法”。

最简单的方法是定义标记和分隔符以及相应的解析器。

例如,您可以定义一条由单个字符组成的消息,并且多条消息将由一个或多个空格分隔。然后你可以通过网络发送这个:

ABCDE

于 2013-07-12T14:54:54.123 回答