fpsockets error: 10047 lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- senglit · 2025-12-10 03:18 Hi, I'm trying to learn how to use fpsockets unit in FPC trunk (win11, FPC 3.3.1, lazarus 4.4). But I am stopped at the first step. Please see the code bellow, which I copied from tfpsock2.pp 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 procedure TForm1.Button1Click(Sender: TObject);var  mySock:TFPSocket;   Received: TReceiveFromStringMessage;  ClientError:string = '';begin  try    mySock := UDPSocket(stIPv4);    try      Sleep(50);      SendStrTo(mySock, '127.0.0.1', 1337, 'hello from client');      Sleep(50);      Received := ReceiveStrFrom(mySock, 16);    finally    CloseSocket(mySock);    end;    if Received.Data <> 'hello from server' then      ClientError := 'Unexpected response: ' + Received.Data;  except on E: Exception do    ClientError := 'Exception: ' + E.Message;  end;end;     It got error at SendStrTo. Please find the error message in the attached. Anything wrong with this fpsockets unit? Or I used it in a wrong way? BTW, I got some other questions about fpsockets: 1. The ReceiveStr function does not have a parameter like TimeOut. So will it be blocked until a string come? 2. I guess SetNonBlocking(sock, True) and ReceiveStrFromNonBlocking will check if there any data in the buffer, and it will return immediately if the buffer is empty, And then I need to implement timeout function by myself, Am I right? 3. How to enlarge the buffer size? ---------------------------------------------------------------------- Thaddy · 2025-12-10 09:55 You should really look at the examples in./packages/fcl-net/examples , not copy code from irrelevant sources. The examples are clear and all work. It would solve your question(s) in minutes. ---------------------------------------------------------------------- senglit · 2025-12-10 10:27 There is no example of fpsockets unit in ./packages/fcl-net/examples. I used the code from https://gitlab.com/freepascal.org/fpc/source/-/blob/main/packages/fcl- net/tests/tfpsock2.pp?ref_type=heads Edit: Also can be found in .packages/fcl-net/tests/tfpsock2.pp Quote from: Thaddy on December 10, 2025, 10:55:05 amYou should really look at the examples in./packages/fcl-net/examples , not copy code from irrelevant sources. The examples are clear and all work. It would solve your question(s) in minutes. ---------------------------------------------------------------------- rvk · 2025-12-10 10:54 Quote from: senglit on December 10, 2025, 11:27:07 amThere is no example of fpsockets unit in ./packages/fcl-net/examples. I used the code from https://gitlab.com/freepascal.org/fpc/source/-/blob/main/packages/fcl- net/tests/tfpsock2.pp?ref_type=heads Did you use the exact code ???? Didn't you mis a Bind() call in there ??? Sorry, that's for server. ---------------------------------------------------------------------- tetrastes · 2025-12-10 11:05 Quote from: senglit on December 10, 2025, 04:18:59 amAnything wrong with this fpsockets unit? Or I used it in a wrong way? Yes you do. Study the test file you cited, especially 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 procedure RunTest(const TestName: String; ASrv, ACli: TProcedure); and try to understand how it works. ---------------------------------------------------------------------- senglit · 2025-12-10 11:21 Quote from: tetrastes on December 10, 2025, 12:05:44 pmQuote from: senglit on December 10, 2025, 04:18:59 amAnything wrong with this fpsockets unit? Or I used it in a wrong way? Yes you do. Study the test file you cited, especially 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 procedure RunTest(const TestName: String; ASrv, ACli: TProcedure); and try to understand how it works. the procedure RunTest seems doesn't do anything special but to run both server procedure and client procedure as threads so that they can communicate to each other. If the client procedure it's self can not run properly how the thread can run? Actually the unit tfpsock2.pp was reported the same error as I posted. ---------------------------------------------------------------------- tetrastes · 2025-12-10 11:42 Well... It's a trunk... In my one-year-old trunk it's OK: D:\Programs\fpcup32-trunk\fpcsrc\packages\fcl- net\tests>D:\Programs\fpcup32-trunk\fpc\bin\i386-win32\ppcrossx64.exe tfpsock2.pp -O3 Free Pascal Compiler version 3.3.1-16887-g7ac4e38b71 [2024/11/12] for x86_64 Copyright (c) 1993-2024 by Florian Klaempfl and others Target OS: Win64 for x64 Compiling tfpsock2.pp fpsockets.pp(870,7) Warning: Unreachable code fpsockets.pp(870,7) Warning: Unreachable code fpsockets.pp(875,3) Warning: Unreachable code fpsockets.pp(897,7) Warning: Unreachable code fpsockets.pp(904,3) Warning: Unreachable code fpsockets.pp(804,7) Warning: Unreachable code fpsockets.pp(837,7) Warning: Unreachable code Linking tfpsock2.exe 564 lines compiled, 0.3 sec, 221072 bytes code, 10564 bytes data 7 warning(s) issued D:\Programs\fpcup32-trunk\fpcsrc\packages\fcl-net\tests>tfpsock2.exe Testing IPv4Test... Success! Testing IPv6Test... Success! Testing DualStackTest... Success! Testing DataAvailableTest... Success! Testing ReceiveArrayTest... Success! Testing ChunkTest... Success! Testing UDPFragmentationTest... Success! Testing NonBlockingTest... Success! Testing FragmentationTest... Success! Testing FragmentedArrayTest... Success! ---------------------------------------------------------------------- cdbc · 2025-12-10 11:50 Hi Yes the demoes work just fine, albeit - they are smeared in /generics/, that's where the "Unreachable code" stems from... · Build yourself a working UDP-server first, thats dead easy... · Then create the client while having the server running in the background... Because the demos uses threads, you don't notice that you have to have the 2 client and server separated. Build 2 apps, by far the easiest approach. ...And keep the generics to a minimum -- You'll thank me later! Regards Benny ---------------------------------------------------------------------- senglit · 2025-12-10 12:25 Quote from: cdbc on December 10, 2025, 12:50:46 pmHi Yes the demoes work just fine, albeit - they are smeared in /generics/, that's where the "Unreachable code" stems from... · Build yourself a working UDP-server first, thats dead easy... · Then create the client while having the server running in the background... Because the demos uses threads, you don't notice that you have to have the 2 client and server separated. Build 2 apps, by far the easiest approach. ...And keep the generics to a minimum -- You'll thank me later! Regards Benny I think it's nothing to do with server and client. It's udp. the client can work without a server and the message sent by client will just be eaten by the network and the client can not receive any response from the server. But it should not report an error. I also build tfpsock2.pp in fpc trunk and lazarus-stable. It does not work. It seems something wrong with the trunk in this state. My trunk version was installed by fpcupdeluxe 2 days ago. I failed while install and report the bug and it's fixed in 20 minutes then I can install. I guess something in lower layer of fpsockes is modified. ---------------------------------------------------------------------- rvk · 2025-12-10 12:27 This might be a problem in latest trunk (and the one from 1 or 2 months ago). In my november trunk 64 bit the test-project gave the same error. I freshly compiled a new 32 bit trunk and that one fails too. 32 bit trunk from july is ok. So this is definitely a bug. You can submit a bugreport. ---------------------------------------------------------------------- Thaddy · 2025-12-10 17:33 Quote from: cdbc on December 10, 2025, 12:50:46 pmHi Yes the demoes work just fine, albeit - they are smeared in /generics/, that's where the "Unreachable code" stems from... No, the unreachable code warnings stem from the callbacks that the compiler "sees" as unreachable. This has always been the case. Since the callbacks are basically pointer types, this is extremely difficult/impossible to solve. Anyway: I tested the examples this morning - not all of them - with a trunk from today on 64 bit Linux and the tested ones simply work. @tetrates A trunk from a year old is not a trunk but you can use it to find the regression. In general there have been quite a few changes this year, so it is perfectly possible that 32 bit platforms are affected. But I refuse to build for 32 bit on 64 bit platforms. ---------------------------------------------------------------------- rvk · 2025-12-10 17:39 Quote from: Thaddy on December 10, 2025, 06:33:52 pmAnyway: I tested the examples this morning - not all of them - with a trunk from today on 64 bit Linux and the tested ones simply work. Seeing the image in opening post OP is on Windows. I tested it with a fresh trunk for Windows and there it fails. A trunk version from July worked fine. So it's because of changes since then. ---------------------------------------------------------------------- Thaddy · 2025-12-10 18:25 Yes if it is a regression it needs a git bisect based on date, not commit (also, add OS and bitness to bug report, so Windows 32 bit). That's the fastest way to find it, but I am not a git guru. ---------------------------------------------------------------------- bytebites · 2025-12-10 20:58 Quoteaaa50b7cbda6ba65b58320be7eff4e5f128f4100 is the first bad commit commit aaa50b7cbda6ba65b58320be7eff4e5f128f4100 Author: Michaël Van Canneyt Date:   Mon Jul 28 22:48:29 2025 +0200     * Amiga needs exact address size.  packages/fcl-net/src/fpsockets.pp | 91 ++++++++++++++++++++++++++-------------  packages/fcl-net/src/ssockets.pp  | 28 +++++++++---  2 files changed, 84 insertions(+), 35 deletions(-) ---------------------------------------------------------------------- tetrastes · 2025-12-11 08:15 Quote from: Thaddy on December 10, 2025, 06:33:52 pm@tetrates A trunk from a year old is not a trunk... And what is it? Release? ---------------------------------------------------------------------- rvk · 2025-12-11 08:27 Quote from: tetrastes on December 11, 2025, 09:15:16 amQuote from: Thaddy on December 10, 2025, 06:33:52 pm@tetrates A trunk from a year old is not a trunk... And what is it? Release?  It's "trunk from a year ago"  I agree you shouldn't say you are on "trunk" (if it's an old compile) because that implies the latest development version, but you can say "trunk from a year ago". But it's best to also specify a date or month so you indicate that there could have been a regression since that version. You can also copy paste the compile info in the about. Lazarus 4.99 (rev main_4_99-2435-g769b7fdc4f) FPC 3.3.1 x86_64-win64-win32/win64 Which will be: Revision: 769b7fdc4f4838e178f91178ce3c7cb482306b85 Date: 30-07-2025 12:04:22 ---------------------------------------------------------------------- tetrastes · 2025-12-11 08:35 I had written this quite clearly: Quote from: tetrastes on December 10, 2025, 12:42:20 pmIn my one-year- old trunk it's OK: had not I? If I thought that more exact info was relevant, I would added it. ---------------------------------------------------------------------- Thaddy · 2025-12-11 08:48 Thanks bytebites. That will help. ---------------------------------------------------------------------- rvk · 2025-12-11 08:50 Quote from: tetrastes on December 11, 2025, 09:35:43 amI had written this quite clearly: Quote from: tetrastes on December 10, 2025, 12:42:20 pmIn my one-year- old trunk it's OK: had not I? You did, which was correct. ---------------------------------------------------------------------- Warfley · 2025-12-12 12:32 Error Code 10047 is WSAEAFNOSUPPORT: 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 Address family not supported by protocol family.    An address incompatible with the requested protocol was used. All sockets are created with an associated address family (that is, AF_INET for Internet Protocols) and a generic protocol type (that is, SOCK_STREAM). This error is returned if an incorrect protocol is explicitly requested in the socket call, or if an address of the wrong family is used for a socket, for example, in sendto. This means that something is wrong internally with the conversion of the TNetworkAddress to the TAddressUnion block used for the sockets call. Need to look into that hopefully somewhen this weekend. (Also I'm pretty sure when reading the code just now of ReceiveStr I've found a bug anyways) To answer the other questions: Quote1. The ReceiveStr function does not have a parameter like TimeOut. So will it be blocked until a string come?ReceiveStr does not read a string per-se, it will just read what is available and return this byte sequence as a string. With Datagrams it's rather simple because you'll just receive the contents of the last datagram as a string. With a Stream (TCP) it's a bit more complicated, because if you received multiple sends since the last receive, you'll just read everything the OS has buffered so far Quote2. I guess SetNonBlocking(sock, True) and ReceiveStrFromNonBlocking will check if there any data in the buffer, and it will return immediately if the buffer is empty, And then I need to implement timeout function by myself, Am I right?Yeah if you want to deal with non blocking sockets. Otherwise look at these functions: 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 // Timeout in MS{$IFDEF HAVE_SELECT_CALL}function DataAvailable(const SocketArray: specialize TArray; TimeOut: Integer = 0): specialize TArray; overload;function DataAvailable(const ASocket: TFPSocket; TimeOut: Integer = 0): Boolean; overload; //inline;function DataAvailable(const SocketArray: array of TFPSocket; TimeOut: Integer = 0): specialize TArray; overload; inline;{$ENDIF} function BytesAvailable(const ASocket: TFPSocket): SizeInt; I'm also pretty sure I made a test for it somewhere, but I'm not sure where 😅 Quote3. How to enlarge the buffer size?Do you mean the OS buffer? If so, fpsockets is just a very thin layer over the berkley sockets API. Not all functionalities are provided by it, and in those cases you just need to do the raw ioctl calls (or other apis). Just grab the FD field of the TFPSocket record and pass it to the system APIs. Note: You can find a few more examples over here (it's the repository before I contributed it to fcl, so it may be slightly outdated): https://github.com/Warfley/PasSimpleSockets/tree/master/examples ---------------------------------------------------------------------- rasberryrabbit · 2025-12-13 15:03 I post small patch on bug tracker. https://gitlab.com/- /project/28644964/uploads/fc662c10d49e4e69cabfe52980160c5b/41536_fpsoc kets.pp.diff There are missing update socket addr record on functions at last trunk updating. With my patch. pass tfpsock2. QuotePS F:\fpcupdeluxe\fpcsrc\packages\fcl-net\tests> ./tfpsock2 Testing IPv4Test... Success! Testing IPv6Test... Success! Testing DualStackTest... Success! Testing DataAvailableTest... Success! Testing ReceiveArrayTest... Success! Testing ChunkTest... Success! Testing UDPFragmentationTest... Success! Testing NonBlockingTest... Success! Testing FragmentationTest... Success! Testing FragmentedArrayTest... Success!