[fpc-pascal] FPConnect Timeout

d.ioannidis at nephelae.eu d.ioannidis at nephelae.eu
Wed Jun 8 20:50:54 CEST 2022


Hi,

On 2022-06-08 21:07, James Richters via fpc-pascal wrote:
> 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?

< 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,

-- 
Dimitrios Chr. Ioannidis


More information about the fpc-pascal mailing list