Solved

Can't handle the ESocketConect exception of a TClientSocket.

Posted on 1998-10-20
14
305 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:jasg
Comment Utility
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
Comment Utility
So what is your code and problem then?
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
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
Comment Utility
Hi, Zif

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

Regards,
Wang
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:jasg
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
If this is the problem, the error event might not be triggered, I'm not sure.
0
 

Expert Comment

by:mtieland
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now