Suggestions on writing a TCP IP messaging system (Client/Server) using Delphi 2010

I would like to write a messaging system using TCP IP in Delphi 2010. I would like to hear what my best options are for using the standard delphi 2010 components/indy components for doing this.

I would like to write a server which does the listening and forwarding of messages to all machines on the network running a client.

1.) a.) clients can send a message to server to be forwarded to all other clients b.) clients listen for messages from other senders (via server) and displays messages.

2.) a.) Server can send a message to all clients b.) Server forwards any messages from clients to all other clients

NOTE: I am not writing a instant messaging or chat program. This is merely a system where users can send alerts/messages to other users - they can not reply to each other!

NO commercial, shareware, etc links - please!

I would like to hear about how you would go about writing this type of system and what approachs you would take, and possibly the TCP IP messaging arcgitecture you would use. Whether it be staright Winows API, Indy components, etc, etc.

thanks for any suggestions

Looking_4_AnswersAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jimyXCommented:
You can use TServerSocket and TClientSocket (It is not located in the Tool Palette by default you have to import it by adding the Socket Package [Component -> Install Packages... -> Add and find "dclsockets120.bpl" in the Delphi bin folder] it will be placed in the Internet category:

 
// Server
procedure TForm1.FormCreate(Sender: TObject);
begin
  ServerSocket1.Port := 657; // just an example
  ServerSocket1.Active := true;
end;

// Server sends to all Clients
procedure TForm1.ButtonSrvrSendClick(Sender: TObject);
var
  i:integer;
begin
  for i := 0 to ServerSocket1.Socket.ActiveConnections - 1 do
    begin
      ServerSocket1.Socket.Connections[i].SendText('Msg');
    end;
end;

// Server reads what client sent and broadcast to all clients
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
  Bfr: PChar;
  i:integer;
begin
  Bfr := PChar(Socket.ReceiveText);
  // to receive from the sending client
  Showmessage(bfr + ' Received from: '+ Socket.RemoteAddress);
  // to send to this sending client only
  Socket.SendText('Reply');

  // to send to all clients
  for i := 0 to ServerSocket1.Socket.ActiveConnections - 1 do
    begin
      if ServerSocket1.Socket.Connections[i].Handle <> Socket.Handle then // to avoid sending to the sender client
        ServerSocket1.Socket.Connections[i].SendText(bfr);
    end;
end;

Open in new window


 
// Client
procedure TForm1.FormCreate(Sender: TObject);
begin
  ClientSocket1.Address := '127.0.0.1'; // The server IP address
  ClientSocket1.Port := 657; //must be same port as the server
  ClientSocket1.Active := True;
end;

// Catch connection errors
procedure TForm1.ClientSocket1Error(Sender: TObject;
  Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
  if ErrorCode = 10061 then
    begin
      Showmessage('Error: Make sure the server is running!');
    end;
  ErrorCode := 0;
end;

// Client sends to the server
procedure TForm1.ButtonClntSendClick(Sender: TObject);
var
  Bfr: PChar;
begin
  Bfr := PChar('Msg');
  ClientSocket1.Socket.SendBuf(Bfr^, length(Bfr));
end;

// Client read the sent message
procedure TForm1.ClientSocket1Read(Sender: TObject;
  Socket: TCustomWinSocket);
var
  Len: Integer;
  Bfr: PChar;
  StrStrm : TStringStream;
begin
  StrStrm := TStringStream.Create;
  Len := Socket.ReceiveLength;
  GetMem(Bfr, Len);
  try
    Socket.ReceiveBuf(Bfr^, Len);
    StrStrm.Write(Bfr^, Len);
    Showmessage(StrStrm.DataString);
  finally
    StrStrm.Free;
  end;
end;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.