Is there any way to specify a shorter timeout for FPConnect?
Mailing lists / fpc-pascal / 2022-06
FPConnect Timeout
4 messages · 3 people ·
- JJames Richters 2
- DDimitrios Chr. Ioannidis 1
- UUnknown author 1
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Hi,
Στις 8/6/2022 7:27 μ.μ., ο/η James Richters via fpc-pascal έγραψε:
Is there any way to specify a shorter timeout for FPConnect?
AFAIK, socket connect timeout is implemented in ssockets ( in TSocketStream ) not in sockets unit.
You can use the TInetSocket's class from ssockets unit ( this is what fphttpclient uses ) overloaded constructor with the aConnectTimeout parameter or if you want you can use the ConnectTimeout property before connect and you're ready to go ...
regards,
I'm not quite following how I could implement that.
Here is what I am trying to do:
Uses
Serial,Sysutils,Sockets,CRT;
var
Socket_Address: TSockAddr;
opt: LongWord;
Connect_Result : Integer;
TCP_Connect_Socket : Tsocket;
begin
Modbus.Device[Device_Number].TCP_Socket := fpsocket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if Modbus.Device[Device_Number].TCP_Socket = TSocket(INVALID_SOCKET) then
Writeln('Error Creating Socket: ',SocketError);
Socket_Address.sin_family := AF_INET;
Socket_Address.sin_addr := StrToNetAddr(Modbus.Device[Device_Number].Connection);
Socket_Address.sin_port := htons(Modbus.Device[Device_Number].TCP_Port);
Connect_Result := fpconnect(Modbus.Device[Device_Number].TCP_Socket, @Socket_Address, sizeof(Socket_Address));
if Connect_Result = SOCKET_ERROR then
Writeln('Error Connecting Socket: ',SocketError); // I want to get this error in 2 seconds
...Are you saying to use TInetSocket instead of the Sockets unit? Or do I just figure out how to use set ConnectTimeout and then that's the timeout that will be used, and still use everything else the same?
James
Earlier message 21 lines
Hi,
James Richters wrote 32 lines
< snip >
( did not test the code )
Assuming that Modbus.Device[xx].TCP_Socket is a TSocket you'll need to change it to TInetSocket, i.e. something like this :
====================================
Modbus.Device[Device_Number].TCP_Socket :=
TInetSocket.Create(Modbus.Device[Device_Number].Connection,
Modbus.Device[Device_Number].TCP_Port);
Modbus.Device[Device_Number].TCP_Socket.ConnectTimeout := 1000;
try
Modbus.Device[Device_Number].TCP_Socket.Connect;
except
{ handle the seConnectTimeOut, seConnectFailed, seHostNotFound etc
errors }
end;
====================================or
====================================
try
Modbus.Device[Device_Number].TCP_Socket :=
TInetSocket.Create(Modbus.Device[Device_Number].Connection,
Modbus.Device[Device_Number].TCP_Port, 1000);
except
{ handle the seConnectTimeOut, seConnectFailed, seHostNotFound etc
errors }
end;
Modbus.Device[Device_Number].TCP_Socket.Disconnect;
Modbus.Device[Device_Number].TCP_Socket.Free;
====================================You could check the TInetSocket examples at
https://gitlab.com/freepascal.org/fpc/source/-/tree/main/packages/fcl-net/examples ...
regards,
