Socket lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- proxy3d · 2012-10-27 17:29 I made some emulation WinInet (functions work with HTTP, in the future add other functions) on Socket for Cross-platform for different OS (easier to port projects with Delphi to Lazarus). I now ported to Linux. I have a problem with sockets: Code: [Select]    ASocket := fpsocket(AF_INET, SOCK_STREAM, IPPROTO_IP);     if ASocket  = INVALID_SOCKET then       exit;       tv.tv_sec        := CONNECT_TIMEOUT div 1000;       tv.tv_usec      := (CONNECT_TIMEOUT mod 1000)*1000;       if fpsetsockopt(ASocket, SOL_SOCKET, SO_RCVTIMEO, @tv, SizeOf(tv)) = SOCKET_ERROR  then         exit;     arg := fpioctl(ASocket , F_GETFL, 0);     narg := arg or O_NONBLOCK;     fpioctl(ASockett, F_SETFL, narg); Return: arg = -1 (error) I want to include non-blocking sockets. Analog of Windows: Code: [Select]     arg: = 1;      ioctlsocket (hConnect ^. Socket, FIONBIO, arg); I use Lazarus 1.0.2, OS: linux ---------------------------------------------------------------------- Laksen · 2012-10-27 17:50 Shouldn't you be using FpFcntl instead of FpIoctl? ---------------------------------------------------------------------- proxy3d · 2012-10-27 18:30 Yes, you are right. Now I have another problem. Code: [Select]    if fpconnect(hConnect^.Socket, @hConnect^.SockAddr, SizeOf(TSockAddr)) = SOCKET_ERROR then     begin       ErrorCode := SocketError; <------- ESysEINPROGRESS       if ErrorCode = ESysEWOULDBLOCK then       begin         try_conn   := CONNECT_TIMEOUT_TRY;         repeat           fpFD_ZERO(FDSetW);           fpFD_SET(hConnect^.Socket, FDSetW);           fpFD_SET(hConnect^.Socket, FDSetE);           tv.tv_sec  := CONNECT_TIMEOUT div 1000;           tv.tv_usec := (CONNECT_TIMEOUT mod 1000)*1000;           fpselect(0, nil, @FDSetW, @FDSetE, @tv);           if fpFD_ISSET(hConnect^.Socket, FDSetW) > 0 then           begin             Result := True;             break;           end;           Dec(try_conn);         until (try_conn = 0) or hConnect^.Abort^;       end;     end else       Result := True; What does SocketError = System error: Operation now in progress? How to work with non-blocking sockets? My code is not correct? ErrorCode = ESysEINPROGRESS ---------------------------------------------------------------------- Laksen · 2012-10-27 18:56 Read about EINPROGRESS here: http://linux.die.net/man/2/connect Also more perhaps relevant info: http://stackoverflow.com/questions/1097771/getsockopt-returns- einprogress-in-non-blocking-connectselect-flow ---------------------------------------------------------------------- proxy3d · 2012-10-27 19:07 I saw this page and many other pages. Different sources I've seen different code. I look at the code (it is processed by the error): http://www.gnu-darwin.org/www001/ports-1.5a-CURRENT/devel/fpc-fcl- net/work/fpc-2.2.0/packages/fcl-net/src/fpsock.pp In C + + written by others. But I have not found an example for Lazarus to work with non-blocking sockets (only using the other components). I need a working example of any connection to FreePascal and Lazarus under Linux with non-blocking sockets. ---------------------------------------------------------------------- Leledumbo · 2012-10-27 22:52 QuoteI need a working example of any connection to FreePascal and Lazarus under Linux with non-blocking sockets.lNet is non-blocking, Synapse also supports it (though not as full). ---------------------------------------------------------------------- proxy3d · 2012-10-27 23:47 I solved the problem. Here is an example of working code for non- blocking sockets: http://www.mail-archive.com/fpc- pascal@lists.freepascal.org/msg16256.html But I fpselect (0, ...) instead fpselect (Socket +1, ...). It works. Soon I'll post a working version of WinInet under Linux (with partial support HTTP). In the future I may have it to FTP and other modes. I wrote WinInet-Linux, because I had to move my components from Delphi to Lazarus-Linux. If need a working connection code non-blocking sockets, I can write my code. ---------------------------------------------------------------------- jomixl · 2013-02-04 11:40 hello, is your code also running on raspi (arm) ? regards joachim ---------------------------------------------------------------------- proxy3d · 2013-05-13 06:50 no know ---------------------------------------------------------------------- proxy3d · 2013-05-13 21:26 I used the standard tools for working with sockets in Lazarus. The module should work if Lazarus for ARM work function fpsend, fpconnect etc. After a few weeks, I'll get FriendlyARM and then I can test the module. The module will be available for free. While it emulation, only the functions necessary for me to work with the Internet via HTTP. There are no functions work with FTP and other protocols. ---------------------------------------------------------------------- Grohlfer · 2013-05-31 01:56 Proxy3D, what eaxctly do you mean by using standard tools for working with sockets in Lazarus? I need to build a simple socket server that would listen to a port and accept client connections, but I'm having difficulties in finding socket components to work with. Thanks, Cesar ---------------------------------------------------------------------- Leledumbo · 2013-05-31 03:31 QuoteI need to build a simple socket server that would listen to a port and accept client connections, but I'm having difficulties in finding socket components to work withThe lowest level: http://www.freepascal.org/docs-html/rtl/sockets/index.html Nice feature rich wrapper: fcl-net (no doc, sample use: http://pascalgeek.blogspot.com/2012/06/encryption-decryption-and- asynchronous.html) ---------------------------------------------------------------------- Silvio Clécio · 2013-05-31 03:43 Nice sample with sockets: https://bitbucket.org/silvioprog/tcpipcomp It will moved to github (https://github.com/silvioprog) soon. ---------------------------------------------------------------------- Grohlfer · 2013-06-01 18:44 Leledumbo and Silvioprog, I tried sockets but when the program runs it gets an exception: Semaphore init failed (possibly too many concurrent threads). Thanks, Groh ---------------------------------------------------------------------- Silvio Clécio · 2013-06-01 18:50 In yourt .lpr, declare cthreads: Code: [Select]program yourproject; {$mode objfpc}{$H+} uses   {$IFDEF UNIX}   cthreads,   {$ENDIF} ... ---------------------------------------------------------------------- Grohlfer · 2013-06-01 19:44 Silvio, After including Cthreads it now gives an exception immediately after pressing run button. I also tried you demo files and components, but they do not compile without errors. Perhaps it is my Lazarus version fault. I'm running IDE v1.0.8 and FPC 2.6.2 Any other ideas? Thanks, Cesar ---------------------------------------------------------------------- Grohlfer · 2013-06-01 20:02 Silvio, I corrected the cthread uses clause. Now it is on the LPR file and everything compiles well. The point is that when the program runs it should be listening to port 23 (telnet), enabling me to telnet to that port, but when I use telnet client and try to connect it comes with "connection refused" message. I certified that there's no telnet daemon running on my server, just my application. I'm using a MAC OS Lion machine. Thanks, Cesar ---------------------------------------------------------------------- taazz · 2013-06-01 20:51 check your firewall, I have no experience with MACOS and the only thing I know is that is based roughly on the same design as the linux kernel so I assume that it has something along the line of the iptables so check that the port is not blocked. ---------------------------------------------------------------------- Silvio Clécio · 2013-06-01 22:50 Port 1024 or lower are only accessible for root. Run your project as root. ---------------------------------------------------------------------- BigChimp · 2013-06-02 03:42 Quote from: taazz on June 01, 2013, 10:51:29 pmI have no experience with MACOS and the only thing I know is that is based roughly on the same design as the linux kernel so I assume that it has something along the line of the iptables so check that the port is not blocked. Ehm OSX is Unix, so yes there is a resemblance. OSX uses a modified Mach kernel and BSD userland tools, so it feels like a regular commercial Unix (to me) - no standard bash though it is available. They also added their own command line tools (and obviously) GUI/applications. Not only could the firewall be the problem, on *nix you need to be root to listen on ports < 1024, so you may need to enable some privileges for your program (however that's arranged for OSX)