views:

119

answers:

1

Hi,

I have built a iPhone application, which capture the voice and streamed to server using NSOutputStream instance. Once the stream stops, iPhone sends a text message as "---end---" to the server and starts to listing on NSOutputStream. When server(built using C#) captured the "---end---" message, it does some processing and write back the text to the client. Once client receive the end message it close NSOutputStream and NSInputStream connections.

Every time, before send the "end" message I put the application to sleep for 0.5 sec. Then I can guaranteed the end message is not mix with other data. This system works well with the simulator and in my network (100Mbps).

However, when I connected the whole application to slow network (1.2 Mbps), the whole communication went vanished. Sometimes the end message is mixing with voice data as similarly the end message is concatenated with the text sending from the server. But when I change the thread sleeping time for 3 sec in server and client, the error occurrence rate was reduced.

I know that this might be a issue of design my network client-server communication protocol. But I cannot figure out how to fix it exactly.

Can anybody explain me, how can I overcome from these issues ?

+1  A: 

Figure out how to wait for the transfer to complete, instead of adding an arbitrary sleep time.

Ben Voigt
@chatcja This is the definition of a "race condition", and it should be avoided. You should have a class that "manages" the connection, and perhaps fires an event for your application to recieve. If you try to "time" this, it will never work consistently. Hope that helps.
Robert Seder
Thank for the information.But the issue I got initially was, iPhone --> C#sometimes the end message is appended with the voice data.C# --> iPhoneend message is appended with the resulted text.Do you have any ideal to send the end message alone ?
chatcja
When you say "text message", do you mean an SMS, or do you mean it adds some text into the TCP stream? TCP doesn't preserve message boundaries. You have to add your own mechanism for separating components of the conversation, such as prefixing each one with its length.
Ben Voigt