Instant messaging may be a problem because of the big time difference between Jamaica and Austria. I am at GMT+1/+2.
Which Pascal compiler do you use (I am using Delphi 7 Pro)? The TServerSocket and TClientSocket use TCP as transport protocol. Do you need additional features in your Client/Server or only "still alive" and status messages? If the later is true, I would suggest to use UDP, preferable the Indy UDP components, because they are very easy to use.
I don't use TTCPClient and TTCPServer and therefore I do not know the differences to TServerSocket and TClientSocket.
In your first source-code example you are using the TClientSocket with the Read event.
Code:
procedure TForm1.CSOCK1Read(Sender: TObject; Socket: TCustomWinSocket);
var
i : integer;
AMessage : string;
MoreMessage : Boolean;
begin
// If a message (one Socket.SendText() was split into multiple parts, you need more
// than one read event, to process messages
// VSOCKR2 must be "global", you may define it in TForm1 (private)
// instead of : VSOCKR2 := Socket.ReceiveText;
VSOCKR2 := VSOCKR2 + Socket.ReceiveText;
//Because it is possible to receive more than one message
//in one Read event all data in the VSOCKR2 must be parsed
//Therefore it is important to define a message-structure
//We define it this way: CR is our message seperator
// <AnyTextWithoutTheCRChar>+CR (CR = #13)
//Because of our definition, it is not allowed to send messages
//with a CR (Carriage Return) in the message text itself
repeat
MoreMessages := false
//Search for the first message seperator
for i :=1 to length(VSOCKR2) do
begin
//message seperator at position i?
if VSOCKR2[i]=#13 then
begin
//Copy the message from the buffer
AMessage := Copy(VSOCKR2,1,i-1);
//Delete the message from the buffer, including the CR
Delete(VSOCKR2,1,i);
//reenable timer to send a new request to the server
SVATT1.Enabled := true;
//if there is still some data in the VSOCKR2 buffer
//we have to check for another message
if VSOCKR2<>'' then
MoreMessages:=true;
//Perform some work with AMessage like:
if AMessage = 'Response: Server received text.' then
begin
MSCOUNT2 := MSCOUNT2 + 1;
LBMSCOUNT2.Caption := IntToStr(MSCOUNT2);
end;
//onPing return the PING to the sender. We must not
//forget the CR as the message seperator
if AMessage = 'PING' then Socket.SendText(AMessage+#13);
end;
end;
until not MoreMessages;
end;
//The Timer
procedure TForm1.SVATT1Timer(Sender: TObject);
begin
//Send request to server with the message-seperator
if CSOCK1.Socket.Connected = true then
CSOCK1.Socket.SendText('Testing server...'+#13);
//disable Timer
SVATT1.Enabled:=false;
end;
The changes in the Client's read event apply also to the Server's read event and all Servers "Socket.SendText()" must be completed with the CR message seperator.
I can also provide an example of using the indy UDP components, if you want.
HTH
Bookmarks