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.
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.
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.
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?
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?
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.
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
I know you must have been talking about OCXs, I hate them two, but socket components are Delph-native.
Regards,
Wang
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 EstablecerConexionDeServid or;
begin
ObtenerEstructuras;
PrincipalForm.ServerSocket .Port:=Con figuracion Record.Pue rtoDelServ icio;
PrincipalForm.ServerSocket .Active:=t rue;
end;
procedure EstablecerConexionDeClient e;
begin
PrincipalForm.ClientSocket .Host:=Con figuracion Record.Nom breDelServ idor;
PrincipalForm.ClientSocket .Port:=Con figuracion Record.Pue rtoDelServ icio;
PrincipalForm.ClientSocket .Active:=t rue;
end;
begin
if ConfiguracionRecord.TipoDe Conexion=t dcServidor then EstablecerConexionDeServid or;
EstablecerConexionDeClient e;
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.
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 EstablecerConexionDeServid
begin
ObtenerEstructuras;
PrincipalForm.ServerSocket
PrincipalForm.ServerSocket
end;
procedure EstablecerConexionDeClient
begin
PrincipalForm.ClientSocket
PrincipalForm.ClientSocket
PrincipalForm.ClientSocket
end;
begin
if ConfiguracionRecord.TipoDe
EstablecerConexionDeClient
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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.
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.C reate(nil) ;
ClientSok.ClientType:=ctBl ocking;
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.Connecte d) do
begin
DataStream:=TWinSocketStre am.Create( ClientSok. Socket, 30000);
try
FillChar(Data, SizeOf(Data), 0);
while (not Terminated) and (ClientSok.Socket.Connecte d) and Data[0] = #0) do
DataStream.Read(Data,SizeO f(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
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.C
ClientSok.ClientType:=ctBl
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.Connecte
begin
DataStream:=TWinSocketStre
try
FillChar(Data, SizeOf(Data), 0);
while (not Terminated) and (ClientSok.Socket.Connecte
DataStream.Read(Data,SizeO
i:=0;
while data[i] <> #0 do
begin
buffer:=buffer+data[i];
Inc(i);
end;
if copy(buffer,Length(buffer)
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.
the explanation is that I began to understand your problem better while I was writing it.