[fpc-pascal] Basic Sockets in FreePascal

Mark Morgan Lloyd markMLl.fpc-pascal at telemetry.co.uk
Mon Jul 27 09:51:47 CEST 2015

Coyo wrote:
> On Sun, 26 Jul 2015 15:47:52 +0000
> Mark Morgan Lloyd <markMLl.fpc-pascal at telemetry.co.uk> wrote:
>> Michael Van Canneyt wrote:
>>> On Sun, 26 Jul 2015, Coyo wrote:
>>>> I have read on the wiki and documentation that FreePascal does not 
>>>> have any native support for socket handling, and that you need an 
>>>> external third party library to have any support for it.
>>> This is not correct. Of course Free Pascal has native support for socket 
>>> handling ?
> I will link the wiki articles later to demonstrate the difficulty of using
> sockets in FPC. However, I do appreciate the advice.
> How do you import a unit/library in FPC? Is there a .deb package or something
> that I install?

Most of what you need comes as part of FPC. There's a very rough split 
between the Run-time Library (RTL) and Free Class Library (FCL), all you 
need is the  uses  statement.

> When compiling or distributing compiled FPC codebases that use external units 
> or libraries in FPC, how do you go about doing that? I failed to find documentation
> illuminating how one goes about doing this. Any enlightenment would be
> greatly appreciated.

Most of what you need comes as part of FPC.

> I dislike object oriented programming, it makes something that should be
> very simple, compact and portable to something more complex than it really
> has to be. But that's just a personal preference. Thankfully, FPC lets me
> use purely functional programming if I prefer to do so. Java and new Perl
> force object oriented programming down my throat, and is part of the reason
> I have a deep interest in FPC. I want to keep it stupid simple.
> I prefer, when possible, to avoid using external dependencies, and to keep
> all code in a single file. While I am not a supergenius coder or anything, 
> I CAN appreciate elegantly-written code. Compact source code and compact
> binaries are something I can appreciate. Overuse or gratuitous use of 
> external libraries and dependencies disturbs me.

I'm afraid that you're going to have to get used to pulling in units 
from the RTL and FCL, and some of these mandate using OO. If it gives 
you problems then ask for help.

> I don't mean to be irritable, but I've heard "don't reinvent the wheel"
> far too many times, especially when defending centralized or dangerously
> homogenous networks and systems, rather than multiple implementations
> and diversity in network components and systems.

I would very strongly recommend against trying to reimplement a complex 
protocol from scratch. You'll make the same mistakes that early 
implementers made, and your code will be vulnerable to the same attacks.

> One can argue over-reliance on a single implementation of a cryptographic
> library, namely OpenSSL, is actually a security RISK rather than a benefit.
> See: Heartbleed. ONLY OpenSSL-dependent applications, systems, and services
> were affected by that programming failure.


> The IRC bot I have now uses a shell script to invoke it, and establishes
> the connection using netcat and standard input/output. I want to eliminate
> netcat from being a dependency. I can worry about TLS and whatnot later.

If you're invoking netcat as an external program to handle your comms 
you're doing something very badly wrong. Netcat (and equivalents such as 
netpipes, i.e. hose/faucet) is great at what it does which is handling 
scripted socket access, but you really shouldn't be using it as part of 
custom code.

> I'm used to Perl, Python and Ruby, which all have systems of making using
> standard libraries trivial. C doesn't really have anything like that.

I don't much like seeing that, but I suppose the assertion is 
justifiable since you usually need to specify both a .h file and the 
name of a library. For FPC, you only need a  uses  statement, the linker 
side is done automatically.

> I would greatly appreciate a definitive answer on how to go about using
> Synapse for things, since the wiki and docs I have found are either ancient,
> inconsistent, unclear, or incomplete. I would be happy to update the wiki, if
> you want me to.

If there's something that's giving you trouble in the Wiki please give 
us a URL and tell us what the ambiguity or error is.

> The reason I want to have sockets internal to FPC instead of using an external
> tool such as netcat is because I want to use one of the core features of FPC,
> namely portability of code. FPC has incredible code portability. It is trivial
> to compile code for a huge selection of target platforms, and I would prefer
> my code to be as portable as possible, and rely on as few dependencies as
> possible. However, implementing an entire userspace TCP/IP protocol stack 
> in FPC is something I am NOT ready to do at this time, especially for a simple
> IRC automaton. Maybe later...

Do not even dream about trying to implement an IP stack from scratch. 
I've done that sort of thing for very specific purposes (e.g. a test 
program that's trying to work out whether it's still our kit at the 
other end of a modem link, or if somebody has replaced it with something 
running PPP), and I'd probably have done far better by linking in code 
from Slirp or equivalent.

Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]

More information about the fpc-pascal mailing list