Solved

Can't handle the ESocketConect exception of a TClientSocket.

Posted on 1998-10-20
14
337 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…

730 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