PDA

View Full Version : Sockets and SendText .. problem..



Philth
03-02-2003, 07:32 AM
If I do a bunch of SendText's in a row, they become merged when read from the other apps...

Like ..

Socket.SendText('Hello');
Socket.SendText('Whats up?');

And on the other app..

Memo1.Lines.Add(Socket.ReceiveText);

comes out in the program looking like:

HelloWhats up?

... Instead of two seperate lines.. Anyone have any easy and quick ways to do this? I'm building a client/server app that sends commands back and forth, so having it all merged into a continious stream is driving me nuts.. Also it seems to not see the first data sent.. I have to send a few times before the server app starts to actually read it..

cairnswm
03-02-2003, 07:54 AM
I've only used the sockets once before and cant find my code (may be at home).

I created a text record for each item I sent. Something like a:
ID, Text, SeqNum, Break Char
On the recieving end I read the text then broke the string at the Break Char. I then broke the structure into its parts and sent an acknowledgement back - based on the ID. If the message was recieved with an incorrect Sequence Num I replied that a message was missing and made the client resend - it stored each message until acknowledged.

I built a simple word game using this - dont know where it is now though :)

TheLion
03-02-2003, 09:38 AM
Well if you are using TCP you won't have to worry about the lines arriving in the wrong order, however what you are sending is nothing more than two merged words. Most sockets/socketobjects have a buffer so everything they receive is placed on the buffer and when you use the command receive they just take the text off the buffer, so in your case the two lines arrive at about the same time in the buffer and you read them. So what you read from the buffer are two merged lines. One way to solve this might be to supply a CR with the line like this

Socket.SendText('Hello' + Chr(13));
Socket.SendText('Whats up?' + Chr(13));

this will cause the memobox to show the lines seperately (if this doesn't work use Chr(10)).

One thing I'm also asking myself is where your end-character is, however this could be handled by the object, I'm used to working with ICS which also has the option of placing it at the end itself, however I always do it myself...

Hope this helps...