views:

99

answers:

1

i have 2 forms 1 for server another for client after droping ttcpserver on server form and setting its localhost property to 127.0.0.1 and localport property to 55555 and Active property to true i wrote a button1(sendtextbutton) onclick event handler:

procedure TForm2.Button1Click(Sender: TObject);
begin
      TcpServer1.Sendln('message');
end;

then on client form i droped 1 ttcpclient 1 label 2 buttons, set clients remote host property to 127.0.0.1 and remote port to 55555 ,wrote an event handler for connectbutton(button1) :

procedure TForm2.Button1Click(Sender: TObject);
begin
try
TcpClient1.Active := true;
except
showmessage('error');

end;
end;

wrote an onconnect event for ttcpclient : procedure TForm2.TcpClient1Connect(Sender: TObject);

begin
    Label1.Caption := 'connected!';
end;

and then finally an onrecieve event hadler for ttcpclient:

procedure TForm2.TcpClient1Receive(Sender: TObject; Buf: PAnsiChar;
  var DataLen: Integer);
begin
    Label1.caption := TcpClient1.Receiveln();
end;

my client programs caption was supposed to change to 'message'(after i connect and click button on my server form) but it doest am i doing it the wrong way? if yes then how to do it i am trying to send a text message from server to client(Yes a reverse connection!)

note: i am new to world of programming

please help!

+1  A: 

TTcpServer does not store a list of connected connection which make broadcast style messages difficult.

I would recommend switching to TidTcpServer and TidTcpClient. The TidTcpServer component has a Context Property that you can loop through to broadcast messages to the clients similar to what you seem to want to do.

Here some links to examples of using TidTcpServer and TIdTcpClient:

Robert Love
no i do not want to broadcast my message just send it from a single server to a single client is tidtcpserver/tidtcpclient good for that too?
Omair Iqbal
If you have only one client on your server will still benefit from using the Indy Components. But using TTcpServer component you would have to do the Sendln in the OnAccept() event.
Robert Love
is onaccept event fired whenever server is connected to the client?
Omair Iqbal
It should be fired on connect
Robert Love
but cant i send sendln from button1click or something ....is there a way to do this?
Omair Iqbal
also can you point to an example of doing this using any compnent indy,ttcpserver,tserversocket please help i cant make this program
Omair Iqbal
all of these example teach you how to send it from client to server do you know of an example that teaches you to send it from server to client and i think indy is HARD to understand or use(maybe only for bigginers) is there a good alternative to indy,do u have any example based on tserversocket/tclientsocket or ttcpserver/ttcpclientserver,thanks for your help
Omair Iqbal
No examples with TTcpServer and/or TTCPClientServer
Robert Love
is there a free component in jedi vcl(or any other component that your aware of) or or is there a free unit (that your aware of) than can make my task possible? thanks again for your help and patience :)
Omair Iqbal