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

Creating a Delphi7 socket application using the VCL TcpClient and TcpServer components

I am trying to create a very simple sockets application using VCL TcpClient and TcpServer and the VCL library but have not been successful. There is a very simple demo app with Delphi7 that uses the CLX library, but I have not found any examples anywhere that use the VCL library. Does anyone have a simple example that they can share?
0
muggs007
Asked:
muggs007
4 Solutions
 
philipjcCommented:
You might  find it easier to use the TServerSocket and TClientSocket directly.

0
 
muggs007Author Commented:
Yes, and I have seen simple solutions that use the older TServerSocket and TClientSocket components. I just have not seen any socket apps developed with TcpClient or TcpServer.
0
 
gmayoCommented:
What's the advantage of TTCPxxxSocket over TxxxSocket? I have used the latter for several years now and they work fine and are very easy to use.

Geoff M.
0
 
gangsterbossCommented:
i think with delphi 7, TxxxSocket isn't a component it has been replaced with TTCPxxxSocket
0
 
alsantosCommented:
muggs007: I made this sample now, Look this code, you have to create a new applcation, put 2 memo, 3 button, 1 edit , 1 tcpserver and 1 tcpclient.

Button1 will active the server
Button2 will connect the client to the server.
Button3 will send the text on memo2
memo1 will write the text received by the server.

put this code:


// event of tcpserver onAccept
procedure TForm1.TcpServer1Accept(Sender: TObject;
  ClientSocket: TCustomIpClient);
var
  s: string;
begin
  memo1.lines.Add('Remote Host: ' + ClientSocket.RemoteHost +
   ' (' + ClientSocket.RemoteHost + ')');
  s := ClientSocket.Receiveln;
  while s <> '' do
  begin
    memo1.lines.add('Text received: ' + s);
    s := ClientSocket.Receiveln;
  end;
end;

// active the server
procedure TForm1.Button1Click(Sender: TObject);
begin
  TcpServer1.Active := True;
  TcpServer1.LocalPort := '111';
end;

// connect the client to the server
procedure TForm1.Button2Click(Sender: TObject);
begin
  TcpClient1.RemoteHost := edit1.Text;
  TcpClient1.RemotePort := '111';
  if not TcpClient1.Connected then
  begin
    TcpClient1.Connect;
    Button2.Caption:='Disconnect';
  end
  else
  begin
    TcpClient1.Disconnect;
    Button2.Caption := 'Connect';
  end;
end;

// send the text to the server
procedure TForm1.Button3Click(Sender: TObject);
var
  I: Integer;
begin
  if TcpClient1.Connected then
    for I := 0 to memo2.Lines.Count - 1 do
      TcpClient1.Sendln(memo2.Lines[I])
    else showmessage('Not connected.');
end;

Alexandre
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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