Recent

Author Topic: Sample program for Accept not working  (Read 2221 times)

Pyewacket

  • Newbie
  • Posts: 6
Sample program for Accept not working
« on: February 23, 2025, 11:55:00 am »
I am trying to implement a simple TCP server that accepts a single connection.
I am using the sample program found at

 https://www.freepascal.org/docs-html/current/rtl/sockets/fpaccept.html

This example does not compile
server.pas(38,23) Error: Call by var for arg no. 2 has to match exactly: Got "ShortString" expected "sockaddr_in"
server.pas(39,33) Warning: Variable "FromName" does not seem to be initialized
server.pas(49,4) Fatal: There were 1 errors compiling module, stopping

Is the sample wrong or do I miss something? Why does the sample for fpaccept use Accept?

MarkMLl

  • Hero Member
  • *****
  • Posts: 8527
Re: Sample program for Accept not working
« Reply #1 on: February 23, 2025, 12:08:40 pm »
The sample's wrong, raise a bug report against the documentation.

The usage of Accept() with a string is marked deprecated at https://www.freepascal.org/docs-html/current/rtl/sockets/index-5.html . There's one or two situations where you do have to use the "original" functions rather than the ones prefixed fp, but you'd be strongly advised to avoid that unless absolutely necessary.

Working code at e.g. https://github.com/MarkMLl/telnetsrv/blob/main/telnetserver.pas roughly line 720 , but the bottom line is that you need to set up a data structure containing the binary socket address etc.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Pyewacket

  • Newbie
  • Posts: 6
Re: Sample program for Accept not working
« Reply #2 on: February 23, 2025, 04:13:56 pm »
Quote
raise a bug report against the documentation.

How do I do that?


MarkMLl

  • Hero Member
  • *****
  • Posts: 8527
Re: Sample program for Accept not working
« Reply #3 on: February 23, 2025, 04:18:48 pm »
Quote
raise a bug report against the documentation.

How do I do that?

Left a bit... up a bit... bugtracker link.

I was looking around a bit after posting, and I think the only time you need to use Accept() etc. (i.e. without the fp prefix) is when you want to associate a socket with a (text or binary) file. However even then there are problems, since there's no way of specifying whether you want AF_INET or AF_INET6 (etc.), or that you want to use unix-domain sockets (etc.).

I can't remember the rationale for all the fp functions (i.e. rather than going direct to the kernel API), but I've got a very vague recollection that they add various signal handlers (i.e. retry interrupted syscalls etc.).

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Pyewacket

  • Newbie
  • Posts: 6
Re: Sample program for Accept not working
« Reply #4 on: February 24, 2025, 06:13:38 pm »
Quote
raise a bug report against the documentation.

I did as you suggested but erroneously in the FPC project as I was not aware of a separate documentation project.
However, I got an interesting reply:

Quote
The general consensus regarding examples in the documentation seems that they do not have to make sense nor actually have to work. They are present to show how a certain function could be used.

Kind of a strange attitude, especially towards beginners who rely on example code.
Why bother with examples in documentation at all if you can't rely on them.




MarkMLl

  • Hero Member
  • *****
  • Posts: 8527
Re: Sample program for Accept not working
« Reply #5 on: February 24, 2025, 06:57:39 pm »
Kind of a strange attitude, especially towards beginners who rely on example code.
Why bother with examples in documentation at all if you can't rely on them.

I agree, but I'm not a member of the core team.

You've done your bit by raising it in the best possible place, I'm trying to do mine by helping you.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

wp

  • Hero Member
  • *****
  • Posts: 13349
Re: Sample program for Accept not working
« Reply #6 on: February 24, 2025, 08:08:14 pm »
However, I got an interesting reply:

Quote
The general consensus regarding examples in the documentation seems that they do not have to make sense nor actually have to work. They are present to show how a certain function could be used.

Kind of a strange attitude, especially towards beginners who rely on example code.
I cannot believe that this was written by a team member (maybe be a frustrated user?). Can you post the link to your bug report?

MarkMLl

  • Hero Member
  • *****
  • Posts: 8527
Re: Sample program for Accept not working
« Reply #7 on: February 24, 2025, 08:17:53 pm »
I cannot believe that this was written by a team member (maybe be a frustrated user?). Can you post the link to your bug report?

https://gitlab.com/freepascal.org/fpc/source/-/issues/41157

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Sample program for Accept not working
« Reply #8 on: February 24, 2025, 10:29:18 pm »
This example does not compile
Compiles just fine.

Code: Bash  [Select][+][-]
  1. fpc -B server.pas
  2. Free Pascal Compiler version 3.2.2 [2021/05/16] for x86_64
  3. Copyright (c) 1993-2021 by Florian Klaempfl and others
  4. Target OS: Linux for x86-64
  5. Compiling server.pas
  6. server.pas(37,6) Warning: Symbol "Accept" is deprecated
  7. Linking server
  8. 48 lines compiled, 0.0 sec
  9. 1 warning(s) issued
  10.  

Quote
Is the sample wrong or do I miss something? Why does the sample for fpaccept use Accept?
The part that is missing (or actually added/set by your used configuration) is ansistring.

The latter is probably an oversight aka copy-paste error (or in this case referring to the exact same example code).

Yes, good idea, let us make all examples work for all compiler modes/settings and for all platforms. Always nice to start learning about a programing language by requiring to know how exactly the pre-processor works. While at it add proper error handling as well. :P
« Last Edit: February 24, 2025, 10:40:27 pm by TRon »
Today is tomorrow's yesterday.

dbannon

  • Hero Member
  • *****
  • Posts: 3660
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Sample program for Accept not working
« Reply #9 on: February 25, 2025, 03:54:21 am »
Seems strange that the doc page for fpaccept() uses the more direct accept() in its example. Quite different signatures.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Sample program for Accept not working
« Reply #10 on: February 25, 2025, 07:25:26 am »
Both entries of these functions in the documentation simply points to the same example.
Today is tomorrow's yesterday.

Pyewacket

  • Newbie
  • Posts: 6
Re: Sample program for Accept not working
« Reply #11 on: March 02, 2025, 09:36:53 pm »

Quote
Yes, good idea, let us make all examples work for all compiler modes/settings

What compiler mode did you use for your working compile?

I'm not aware of having changed that afterninstall but who knows...



TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Sample program for Accept not working
« Reply #12 on: March 02, 2025, 09:50:39 pm »
What compiler mode did you use for your working compile?
See example, it is explicitly set (for a reason as a compiler mode has further implications).
Today is tomorrow's yesterday.

dbannon

  • Hero Member
  • *****
  • Posts: 3660
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Sample program for Accept not working
« Reply #13 on: March 03, 2025, 11:07:51 am »
TRon, did you test the app once compiled. Seems to me, while it compiled, it does not work as claimed.

My recommendation is the examples in <FPC>packages/fcl-net/examples/isock*

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

tetrastes

  • Hero Member
  • *****
  • Posts: 737
Re: Sample program for Accept not working
« Reply #14 on: March 03, 2025, 12:00:23 pm »

Quote
Yes, good idea, let us make all examples work for all compiler modes/settings

What compiler mode did you use for your working compile?

I'm not aware of having changed that afterninstall but who knows...

This example compiles for unix, but not for windows.
Accept with the second argument String exists in fpcsrc/packages/rtl-extra/src/unix/sockets.pp.

 

TinyPortal © 2005-2018