Link to home
Start Free TrialLog in
Avatar of jasg
jasg

asked on

Can't handle the ESocketConect exception of a TClientSocket.

  When the connection from a tClientSocket to a tServerSocket fails, Delphi 3 won't let my app handle the exception and simply puts up a dialog.

   I've tried setting ErrorCode to 0 in the tClientSocket.OnError event handler but it's never triggered.

   Also, I've tried using a try statement before attempting the connection, but since it's async that doesn't work either.
Avatar of dwwang
dwwang

Please describe your problem in more details, since I never meet such kind of problem, Delphi always triggers the ClientSock.OnError event in my programm, when the server is down or some other reasons.
Heck, still working with these standard Delphi internet components? Why? They need several Dll's and are buggy (ps. not that this means your problem is a bug dough). I would suggest to use F.Piette's freeware components! Easy to use and no need for dll's, OCX, activeX etc... http://www.rtfm.be/fpiette/indexuk.htm

with great examples! Free source and free mailing list...

Zif.
Hi, Zif

I think the socket component in Delphi don't need DLLs, or you mean that F.Piette's component even don't need winsock/wsock32.dll?
Avatar of jasg

ASKER

dwwang is right any program that rely on those components don't need any special dlls to run.  But my code will only connect with win98
So what is your code and problem then?
jasg, dwwang,

offcourse the winsock dll's are still needed. (sorry for the wrong explenation). What I meant was this : The internet components standard with D3 are ActiveX! So you need to install these one also on the other PC.

Winsock dll are mostly already on the computer.

So with F.Piette's you don't have to deal with other dll's then the really nessecary ones. Just install your program and there that's it. On the other hand. When programming with ActiveX, you first have to install these too... and this means sometimes a lot of trouble!

Zif.
Hi, Zif

I know you must have been talking about OCXs, I hate them two, but socket components are Delph-native.

Regards,
Wang
Avatar of jasg

ASKER

Ok here's the details:

The tClientSocket's ClientType is set to ctNonBlocking
The tServerSocket's ServerType is set to stNonBlocking

The code that sets up the connection follows:

procedure EstablecerConexion;

  procedure EstablecerConexionDeServidor;
  begin
    ObtenerEstructuras;

    PrincipalForm.ServerSocket.Port:=ConfiguracionRecord.PuertoDelServicio;
    PrincipalForm.ServerSocket.Active:=true;
  end;

  procedure EstablecerConexionDeCliente;
  begin
    PrincipalForm.ClientSocket.Host:=ConfiguracionRecord.NombreDelServidor;
    PrincipalForm.ClientSocket.Port:=ConfiguracionRecord.PuertoDelServicio;
    PrincipalForm.ClientSocket.Active:=true;
  end;
                                             
begin        
  if ConfiguracionRecord.TipoDeConexion=tdcServidor then EstablecerConexionDeServidor;
  EstablecerConexionDeCliente;
end;

That's it.  The app can be server and client or just client.  It connects just fine.  But if the server is down the OnError exception never gets triggered.  And I can't get the app to connect in Win95.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of mtieland
mtieland

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jasg

ASKER

to mtieland

   As I stated on my initial posting, I've already used the TRY statement but it won't work because the time out happens well after the TRY ... EXCEPT code is executed.  Remember this is async code.  The connection code is in the question's history, look it up.

Thanks
Hi, jasg

See from your code you are using HOST rather than address, but did you either setup the DNS system properly or write the host name in the HOSTS file of win95? If not, just use the address property.
If this is the problem, the error event might not be triggered, I'm not sure.
How can this be ASync? The only thing that happens ASync is data send to your program, for wich you would need a loop in a seperate thread to handle the entire conversation.

If I understand this correctly you try to connect to a server on a sertain port but if the server is unavailable an exception is raised.

The following code is which I use to connect to a SMTP server.
btw, it is a ThreadObject (see wizard of delphi)

procedure SMTPClientThread.Execute;
var ClientSok : TClientSocket;
    DataStream : TWinSocketStream;
    Data : array [0..1023] of char;
    buffer : string;
    i : integer;
begin
 ClientSok:=TClientSocket.Create(nil);
 ClientSok.ClientType:=ctBlocking;
 ClientSok.Port:=25;
 ClientSok.Service:='SMTP';
 try
  ClientSok.Open;
 except on ESocketError
  do begin
      ClientSok.Active:=False;
      Terminate;
     end;
  while (not Terminated) and (ClientSok.Socket.Connected) do
  begin
   DataStream:=TWinSocketStream.Create(ClientSok.Socket, 30000);
  try
   FillChar(Data, SizeOf(Data), 0);
   while (not Terminated) and (ClientSok.Socket.Connected) and  Data[0] = #0) do
    DataStream.Read(Data,SizeOf(Data));
   i:=0;
   while data[i] <> #0 do
   begin
    buffer:=buffer+data[i];
    Inc(i);
   end;
   if copy(buffer,Length(buffer)-1,2)=#13#10
    then begin
          Delete(Buffer, Pos(#13#10, Buffer), 2);
          ProcessCommand(Buffer); {just my subrotine to handle reply's from the server}
          Buffer:='';
         end;
  finally
   DataStream.Free;
  end;
end;

Notice the fact that my connection is ctBlocking!
I am not very fimiliar with the nonblocking methodes. I know for one thing that if you use the blocking methode you are unable to use the normal events of the TClientSocket component but must use try except blocks. When I initialy started working with the TClientSocket component I did use the nonblocking methode and used the OnError event to handle Exceptions wich worked well.
I will look into this matter for you and come back with more lateron this day.

Good Luck,

Martijn

It might be a bit confusing the way I wrote the previos note,
the explanation is that I began to understand your problem better while I was writing it.