Solved

Can't handle the ESocketConect exception of a TClientSocket.

Posted on 1998-10-20
14
329 Views
Last Modified: 2010-04-06
  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.
0
Comment
Question by:jasg
  • 6
  • 3
  • 3
  • +1
14 Comments
 
LVL 4

Expert Comment

by:dwwang
ID: 1343550
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.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1343551
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.
0
 
LVL 4

Expert Comment

by:dwwang
ID: 1343552
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?
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:jasg
ID: 1343553
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
0
 
LVL 4

Expert Comment

by:dwwang
ID: 1343554
So what is your code and problem then?
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1343555
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.
0
 
LVL 4

Expert Comment

by:dwwang
ID: 1343556
Hi, Zif

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

Regards,
Wang
0
 

Author Comment

by:jasg
ID: 1343557
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.
0
 

Accepted Solution

by:
mtieland earned 200 total points
ID: 1343558
It would help if you would show us some of your code...

What always seens to help is put a try..except on ESocketError block arround your code. Whenever a error occures and you need to handle this, most of the time the user connection needs to be dropped for your program to continue, so just close the socket connection.

0
 

Author Comment

by:jasg
ID: 1343559
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
0
 
LVL 4

Expert Comment

by:dwwang
ID: 1343560
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.
0
 
LVL 4

Expert Comment

by:dwwang
ID: 1343561
If this is the problem, the error event might not be triggered, I'm not sure.
0
 

Expert Comment

by:mtieland
ID: 1343562
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

0
 

Expert Comment

by:mtieland
ID: 1343563
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.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question