3

I have 2 forms, 1 for server another for client. After dropping 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 dropped 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!)

Alex
  • 1,574
  • 17
  • 36
Omair Iqbal
  • 1,820
  • 1
  • 28
  • 41
  • Your server is not talking to the client socket. See here for an example: http://stackoverflow.com/questions/1591256/delphi-ttcpserver-connection-reset-when-reading – mjn Jul 06 '10 at 15:40
  • @ mjustin: but my clients label caption turns to connected witch means i am connected :( – Omair Iqbal Jul 06 '10 at 16:10
  • 3
    The client is connected, but the server can only send to it using the right socket. How can `TcpServer1.Sendln()` know which client should receive the data (there can be many clients connected at the same time)? See my link, it uses `ClientSocket.Sendln` on the server side. I can also recommend Internet Direct (Indy) - you will find more code examples for it. – mjn Jul 06 '10 at 16:24
  • i dont understand how will ClientSocket.Sendln on the server side know which client to send to i mean should'nt it be something like clientsocket1.sendln ,clientsocket2.sendln ,and i find indy hard to understand and use is there a good alternative to indy? – Omair Iqbal Jul 09 '10 at 06:35
  • On the server side, each client will have its own `TCustomIpClient` object. That object is provided to you in the `TTcpServer.OnAccept` event. Depending on how you are using `TTcpServer` (ie, if you are using the default settings), that is the ONLY place you can access the client's `TCustomIpClient` object from. See my answer for more details. – Remy Lebeau Jul 13 '12 at 21:17

2 Answers2

2

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
  • 12,447
  • 2
  • 48
  • 80
  • 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 Jul 06 '10 at 16:59
  • 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 Jul 06 '10 at 17:16
  • is onaccept event fired whenever server is connected to the client? – Omair Iqbal Jul 07 '10 at 13:15
  • It should be fired on connect – Robert Love Jul 07 '10 at 14:11
  • but cant i send sendln from button1click or something ....is there a way to do this? – Omair Iqbal Jul 07 '10 at 16:30
  • 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 Jul 07 '10 at 16:37
  • 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 Jul 09 '10 at 07:00
  • No examples with TTcpServer and/or TTCPClientServer – Robert Love Jul 09 '10 at 07:17
  • 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 Jul 11 '10 at 11:42
1

Your server code does not work because TTcpServer.SendLn() does not send the data to the client's endpoint of the connected socket. That is why the client never sees the data - it is being sent into limbo.

If the TTcpServer.BlockMode property is set to bmThreadBlocking (which it is by default), or if the TTcpServer.BlockMode is set to anything else and you are manually calliing the parameterless overloaded TTcpServer.Accept() method, then the only place you have access to the client's endpoint is from inside the TTcpServer.OnAccept event. Under these conditions, when that event handler exits, the server will disconnect the client, so any work the server wants to do with the client must be done from inside that event.

If that does not suit your needs, then you will have to set the TTcpServer.BlockMode property to either bmBlocking or bmNonBlocking and then manually call the overloaded TTcpServer.Accept() method that returns a TCustomIpClient object. After the TTcpServer.OnAccept event has been triggered and exited, you will gain ownership of that object and have full control over its lifetime and can then access it whenever and however you want.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770