[SOLVED] Invalid argument with fpsetsockopt & SO_RCVTIMEO lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- russdirks · 2017-08-01 05:31 I'm using the code below to open a socket for UDP communication, and set a receive timeout.  The call to fpsetsockopt to set the timeout works fine for Windows and MacOS, but under Ubuntu, it reports error 22, which I believe is invalid argument.  Anybody have an idea what the problem could be? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     // open the socket   m_socket := fpSocket(AF_INET, SOCK_DGRAM, 0);   m_addrIn.sa_family := AF_INET;   m_addrIn.sin_port := htons(m_portIn);   m_addrIn.sin_addr.s_addr := INADDR_ANY;   res := fpbind(m_socket, @m_addrIn, sizeof(m_addrIn));    ...    // set the timeout   {$IFDEF Windows}   tv.tv_sec := 100;                          // milliseconds   tv.tv_usec := 0;                             // unused   {$ELSE}   tv.tv_sec := 0;                              // seconds   tv.tv_usec := 100 * 1000;            // microseconds   {$ENDIF}    res := fpsetsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO,  @tv, sizeof(tv)); ---------------------------------------------------------------------- russdirks · 2017-08-12 05:38 Still stuck on this one.  I was looking at the source code for the rtl-extra package, and code for fpsetsockopt looks like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function  fpsetsockopt  (s:cint; level:cint; optname:cint; optval:pointer; optlen : tsocklen):cint;begin  fpSetSockOpt:=do_syscall(syscall_nr_Set SockOpt,S,level,optname,TSysParam(optval),optlen);  internal_socketerror:=fpgeterrno;end;  The do_syscall is just a call  to some external code somewhere.  According to this man page the argument for SO_RCVTIMEO is a struct timeval.  My tv variable is declared like so: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TTimeVal = record   tv_sec : uint32;   tv_usec : uint32;end; Anybody have any ideas what the problem might be? ---------------------------------------------------------------------- Thaddy · 2017-08-12 06:08 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TTimeVal = packed record   tv_sec : longint;//time_t   tv_usec : longint;//suseconds_tend; Note the members are signed members and you need a packed record. From the man pages https://linux.die.net/man/2/gettimeofday MSDN https://msdn.microsoft.com/en- us/library/windows/desktop/ms740560(v=vs.85).aspx ---------------------------------------------------------------------- russdirks · 2017-08-12 15:57 Thanks for responding.  I made those changes, but the error still persists. ---------------------------------------------------------------------- engkin · 2017-08-12 17:09 I am a Windows person, so I don't know what's wrong with your code under Linux. But, package fcl-net has DNS query in unit netdb.pp using fpSelect instead of fpsetsockopt. DNS query, you know, should not wait forever: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...fpsendto(sock,@qry,qrylen+12,0,@SA,SizeOf(SA));  // Wait for answer.  RTO:=TimeOutS*1000+TimeOutMS;  fpFD_ZERO(ReadFDS);  fpFD_Set(sock,readfds);  if fpSelect(Sock+1,@readfds,Nil,Nil,RTO)<=0 then    begin    fpclose(Sock);    exit;    end;  AL:=SizeOf(SA);  L:=fprecvfrom(Sock,@ans,SizeOf(Ans),0,@SA,@AL);  fpclose(Sock);... ---------------------------------------------------------------------- marcov · 2017-08-12 17:17 Maybe linux migrated to a 64-bit timeval ? Do you generate 32-bit or 64-bit applications? ---------------------------------------------------------------------- Thaddy · 2017-08-12 18:17 Quote from: marcov on August 12, 2017, 07:17:12 pmMaybe linux migrated to a 64-bit timeval ? Do you generate 32-bit or 64-bit applications? I thought I checked sys/time.h but I will check it again. ---------------------------------------------------------------------- marcov · 2017-08-12 18:22 The best is to compile a small C program that prints sizeof. Though on Linux that is still open to interpretation, since sometimes kernel and userland use different structures there. ---------------------------------------------------------------------- russdirks · 2017-08-12 19:54 @marcov I think you were right.  I got it working!  I changed the type values to PtrInt and now it works.  I'll have to go back to Mac OS X and Windows to see how it works there.  May have to do some $IFDEFs. @engkin  Thanks for the sample code.  Based on that and some things I read in the comments section of this Stack Overflow question, it seems the fpSelect method with a non-blocking sockets is the better way to go.