• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

Problem with ClientSocket - SendText concatenates sent strings


Hello,

My problem is following: I am writing a very simple chat server/client with

a userlist. The userlist is stored on server, and each client should send

it's nickname on connect and get the list of currently talking users. So I

have the following OnConnect code:

procedure TfrmClient.CliSocketConnect(Sender: TObject; Socket:

TCustomWinSocket);
begin
  Msg('[Connected]');
  Socket.SendText(sSysTag + ' ' + sCmdGetUList);
  Socket.SendText(sSysTag + ' ' + sCmdNick + ' ' + edNick.Text);
  btnConnect.Caption := 'Disconnect';
end;

However, the server receives both commands in the single string, like this:

"zsystem getulistzsystem nick Guest"

Eventually my command-recognizing function fails. Is there a way to send

this so server receives 2 different commands, like this:

"zsystem getulist"
"zsystem nick Guest"

Thanks in advance,
Yury.
0
Zaytsev
Asked:
Zaytsev
1 Solution
 
sfockCommented:
how about sending XML instead of flat commands?

I'd use XMLWorks. creating a simple Object to enpackage the info like

TmyObj = class(TXMLObject)
published
  property command : string ...
  property Nickname : string ...
end;

then creating it at the client sending it's xml property to the server
and serverside creating it to and assigning the received XML to it and here you are

you can get XMLWorks as an open Source project from the D7 companion CD or at http://www.xmlworks.de/de/index.php
0
 
keashFCommented:
this happens because te socket can not know when you want your string to end.
i had the same problem some time ago and i solved it by adding a termination symbol at the end of each line (something that is most unlikely to occur in a normal string, like '|'). then, of course, the receiver must separate the received strings.

good lick
keashF
0
 
pderuiterCommented:
why dont you just send the userlist back when you receive a logon? no need for that seperate command is there?
Just send 1 command at a time
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ZaytsevAuthor Commented:
Let's start...

...sfock, I use Delphi 5 and then, I do not want to use XML. Just a simple plain commands.

...keashF, that is what I was thinking about, but I thought that that should be a way to instruct ClientSocket to SEND the message on SendText, but not queque it and wait until there are some other messages to send... And then it will take many time from me to write a message-splitting code :-) I believe there should be a way to avoid cacheing...

...pderuiter, that is not the only situation when I need to send multiple commands from the same function. What do you mean by saying "at a time"?

0
 
sfockCommented:
> I use Delphi 5
that is no problem XMLWorks runs with D5 as well as with 6 or 7

>and then, I do not want to use XML. Just a simple plain commands
okay if you just don't want to ... what can i do? But do you allow me to ask what's so horrible with XML?
0
 
ZaytsevAuthor Commented:
>that is no problem XMLWorks runs with D5 as well as with 6 or 7
That's better. I visited their website and saw that.

Okay, I will try to implement you solution. What's so horrible with XML? Just that I have never worked with it before and most probably I'll have a lot of troubles with it. Besides, that seem to the the only reliable solution.
However, what should I do if SendText would concatenate my two consequent XML objects?
0
 
sfockCommented:
well at first create a class, that exists on both sides client and server like

TRemoteCommand = class(TXMLObject)
 FCommand : string;
 FNickname : string;
 FResultList : TXMLList; // create this in the constructor
published
 property command : string read FCommand write FCommand;
 property Nickname : string read FCommand write FCommand;
 property Resultist : TXMLList read FResultList;
end;

If you are willing to send the command do something like
var
  cmd : TRemoteCommand;
begin
  cmd := TRemoteCommand.create;
  cmd.command := 'getulist';
  cmd.nickname := 'Guest';
  Socket.SendText(cmd.xml);
end;

on the serverside you do something like

var
  cmd : TRemoteCommand;
begin
  cmd := TRemoteCommand.create;
  cmd.xml := Socket.ReceiveText;
  // do with the infos in cmd what ever you like ...
end;

If you want to create the command more flexible or like a batch you may choos a TXMLCollection and TXMLCollectionItem as Transport objects

hope that helps
0
 
ZaytsevAuthor Commented:
'Thanks a lot! I will try that :-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now