Re: [fpc-pascal] FPConnect Timeout
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 ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] FPConnect Timeout
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 -Original Message- From: fpc-pascal On Behalf Of Dimitrios Chr. Ioannidis via fpc-pascal Sent: Wednesday, June 8, 2022 1:11 PM To: fpc-pascal@lists.freepascal.org Cc: Dimitrios Chr. Ioannidis Subject: Re: [fpc-pascal] FPConnect Timeout 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, -- Dimitrios Chr. Ioannidis ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] FPConnect Timeout
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, -- Dimitrios Chr. Ioannidis ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
[fpc-pascal] FPConnect Timeout
Is there any way to specify a shorter timeout for FPConnect? ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal