Using Tcp With Ssl On Tcp Client

Typical TCP client scenario is:

var
  sock: TTCPBlockSocket;
  s: string;
begin
  sock:= ttcpblocksocket.Create;
  try
    sock.Connect('your.server.com','80');
    sock.SendString('yourdata' + CRLF);
    s := sock.recvstring(15000);
//...
  finally
    sock.Free;
  end;
end;

For upgrade existing TCP client code for SSL/TLS mode you must:

  • select SSL plugin for your favourite SSL library by adding plugin unit to your project uses. Without this step is your application compiled without any SSL support! See article about SslPlugin for more details.
  • select point where you want to start SSL/TLS mode. It can be on begin of TCP connection (servers in this mode using non-standard ports). Second option will be different. You start TCP connection without SSL and you can start SSL/TLS mode when you send some special command for start of SSL/TLS mode to server. In Both cases you can start SSL/TLS mode by SSLDoConnect method, but allways you must have existing connected TCP channel.
  • If previous step is successful, then any next communication going through SSL/TLS mode, until you shutdown SSL/TLS mode by SSLDoShutdown or until you close socket.

So, your modified code can look as:

var
  sock: TTCPBlockSocket;
  s: string;
begin
  sock:= ttcpblocksocket.Create;
  try
    sock.Connect('your.server.com','443');
    sock.SSLDoConnect;
    if sock.lasterror <>  0 then //check for success start of SSL
      exit;
    sock.SendString('yourdata' + CRLF);
    s := sock.recvstring(15000);
//...
  finally
    sock.Free;
  end;
end;
public/howto/tcpwithssl.txt · Last modified: 2007/11/30 14:30 (external edit)
Driven by DokuWiki Recent changes RSS feed