Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2020-05

How to implement a simple tcp/ip connection?

17 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Giuliano Colla

Hi all,

I need to implement a simple dedicated TCP/IP connection between a client and a server. Nothing fancy, just sending and receiving short strings. I have used in the past for that purpose the unit Sockets lifting it from Kylix. This unit was taking advantage of libc, and this rules it out.

I see that fpc provides a Socket unit in rtl-extra and a fpSock unit in fcl-net. At first glance they both appear to provide what I need.

Can somebody which has used those units point me in the right direction, before I painfully study in detail those units.

Thanks for any help.

Giuliano

Do not do to others as you would have them do to you.They might have different tastes.
Luca Olivetti
to Giuliano Colla

El 14/5/20 a les 18:21, Giuliano Colla ha escrit:

Show quoted text 7 lines
Hi all, I need to implement a simple dedicated TCP/IP connection between a client and a server. Nothing fancy, just sending and receiving short strings. I have used in the past for that purpose the unit Sockets lifting it from Kylix. This unit was taking advantage of libc, and this rules it out.

If the problem is the unit libc but you can accept that the project is using libc.so the ararat synape is another alternative. I started using it when I abandoned delphi for lazarus and I still use it to this day (mostly windows though).

Bye

Luca
Michael Van Canneyt
to Giuliano Colla: I need to implement a simple dedicated TCP/IP connection between a client and a server.…
Giuliano Colla wrote 13 lines
Hi all, I need to implement a simple dedicated TCP/IP connection between a client and a server. Nothing fancy, just sending and receiving short strings. I have used in the past for that purpose the unit Sockets lifting it from Kylix. This unit was taking advantage of libc, and this rules it out. I see that fpc provides a Socket unit in rtl-extra and a fpSock unit in fcl-net. At first glance they both appear to provide what I need. Can somebody which has used those units point me in the right direction, before I painfully study in detail those units.

The TFTPHTTPClient and server components both use fpSock.

Michael.

Alexander Grotewohl
to Giuliano Colla: I need to implement a simple dedicated TCP/IP connection between a client and a server.…

the sockets unit should work exactly like any tutorial for c sockets for linux. a handful of the functuons need an fp prefix but should work mostly the same.

Alexander Grotewohl https://dcclost.com ________________________________
Earlier message 26 lines
From: fpc-pascal on behalf of Giuliano Colla Sent: Thursday, May 14, 2020 12:21:36 PM To: FPC-Pascal users discussions Subject: [fpc-pascal] How to implement a simple tcp/ip connection? Hi all, I need to implement a simple dedicated TCP/IP connection between a client and a server. Nothing fancy, just sending and receiving short strings. I have used in the past for that purpose the unit Sockets lifting it from Kylix. This unit was taking advantage of libc, and this rules it out. I see that fpc provides a Socket unit in rtl-extra and a fpSock unit in fcl-net. At first glance they both appear to provide what I need. Can somebody which has used those units point me in the right direction, before I painfully study in detail those units. Thanks for any help. Giuliano -- Do not do to others as you would have them do to you.They might have different tastes.
Giuliano Colla
to Bo Berglund

Thank you to all of you guys. Now I have a much better picture

Do not do to others as you would have them do to you.They might have different tastes.
Graeme Geldenhuys
to Giuliano Colla: I need to implement a simple dedicated TCP/IP connection between a client and a server.…
Giuliano Colla wrote
I need to implement a simple dedicated TCP/IP connection between a client and a server.

You can always use the Indy TCP components too. I've used them for years under Delphi and FPC with great success. Huge amounts of protocols are implement and loads of examples available.

SVN: svn.atozed.com
Git (mirror): github.com/graemeg/indy

Regards,
Graeme

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp
Bo Berglund
to Graeme Geldenhuys
Graeme Geldenhuys wrote 11 lines
On 14/05/2020 5:21 pm, Giuliano Colla wrote: > I need to implement a simple dedicated TCP/IP connection between a > client and a server. You can always use the Indy TCP components too. I've used them for years under Delphi and FPC with great success. Huge amounts of protocols are implement and loads of examples available. SVN: svn.atozed.com Git (mirror): github.com/graemeg/indy

Well the simplest way to get Indy10 into Lazarus is via Online Package Manager! Searh for Indy and it will find Indy10 for you. THe package is named indylaz.

That said (I also use Indy a lot) if one converts a program from Delphi where one has used the TServerSocket or TClientSocket classes it is a rather steep climb since Indy10 is entirely blocking and the Delphi components were event driven.

I am myself struggling with this problem right now converting an old Windows service application that is configured from a Delphi programmed client operating over TCP/IP sockets. And it uses TClientSocket and TServerSocket....
I am going to try LNet hoping it will not bee too different from the Delphi native components...
(Note: Delphi 7/Delphi 2007)

Bo Berglund Developer in Sweden
Graeme Geldenhuys
to Bo Berglund
Bo Berglund wrote
Well the simplest way to get Indy10 into Lazarus is via Online Package Manager!

Do they (whoever it might be) keep it up to date with the lastest Indy?

it is a rather steep climb since Indy10 is entirely blocking and the Delphi components were event driven.

Indy devs explain it here. Blocking is not evil.

https://www.swissdelphicenter.ch/en/showarticle.php?id=4

Regards,
Graeme

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp
Bo Berglund
to Graeme Geldenhuys
Graeme Geldenhuys wrote
> Well the simplest way to get Indy10 into Lazarus is via Online Package > Manager! Do they (whoever it might be) keep it up to date with the lastest Indy?

I believe that it is actually Remy Lebeau who is doing that (I might be wrong)..
In any case I received Indy 10.6.2 when I installed through OPM from Lazarus 2.0.8 on Windows.

Show quoted text 6 lines
> it is a rather steep climb since Indy10 is entirely blocking and the > Delphi components were event driven. Indy devs explain it here. Blocking is not evil. https://www.swissdelphicenter.ch/en/showarticle.php?id=4

Yes, I know that it is not evil, but if you are stuck with a rather big Delphi application that is proven to work with the TServerSocket and using events to handle "everything" then it is much more difficult to turn to Indy during porting to FPC and then on to Linux...

As you might have seen in various posts I have made recently I am trying to port a Windows service application written in Delphi7/Delphi2007 running on Windows to FPC/Lazarus in order to then move on to Linux.

I have worked my way through it in Delphi 2007 to add ifdefs in a lot of places in order to prepare for the conversion into Lazarus and now I am in Lazarus trying to weed out the remaining issues. Not close yet...
But it seems like my use of LNet in any case has been accepted by FPC/Lazarus as a replacement for TServerSocket.

Bo Berglund Developer in Sweden
Giuliano Colla
to Bo Berglund: On Thu, 14 May 2020 22:02:01 +0100, Graeme Geldenhuys Well the simplest way to get Indy10…

I'm struggling with a similar problem. It would appear that the easiest way would be just take advantage of the Sockets unit. You only must define some more constants, such as SO_REUSEPORT and SOCK_NONBLOCK which are missing in fpc.

I just made some preliminary tests, and it looks like it's not so difficult to implement something quite compatible with Delphi philosophy. The SOCK_NONBLOCK flag is one of the keys.

Now I'm planning to look in more detail the old Delphi implementation, in order to see how they were tacking advantage of Libc. If you're interested I'll let you know my results.

Giuliano

Il 16/05/2020 20:13, Bo Berglund via fpc-pascal ha scritto:

Show quoted text 7 lines
I am myself struggling with this problem right now converting an old Windows service application that is configured from a Delphi programmed client operating over TCP/IP sockets. And it uses TClientSocket and TServerSocket.... I am going to try LNet hoping it will not bee too different from the Delphi native components... (Note: Delphi 7/Delphi 2007)
Do not do to others as you would have them do to you.They might have different tastes.
Michael Van Canneyt
to Giuliano Colla
Giuliano Colla wrote
I'm struggling with a similar problem. It would appear that the easiest way would be just take advantage of the Sockets unit. You only must define some more constants, such as SO_REUSEPORT and SOCK_NONBLOCK which are missing in fpc.

They are not missing.

How do you think fpsock and lnet implement non-blocking ? It's simply called O_NONBLOCK.

For some reason, SO_REUSEPORT was commented for linux, I uncommented it.

Michael.

Alexander Grotewohl
to Michael Van Canneyt

It was added for Linux 3.9 in 2013.. maybe the code predates that?

Alexander Grotewohl https://dcclost.com ________________________________
Earlier message 23 lines
From: fpc-pascal on behalf of Michael Van Canneyt Sent: Tuesday, May 19, 2020 12:57:37 PM To: FPC-Pascal users discussions Cc: Subject: Re: [fpc-pascal] How to implement a simple tcp/ip connection? On Tue, 19 May 2020, Giuliano Colla wrote: > I'm struggling with a similar problem. It would appear that the easiest > way would be just take advantage of the Sockets unit. You only must > define some more constants, such as SO_REUSEPORT and SOCK_NONBLOCK which > are missing in fpc. They are not missing. How do you think fpsock and lnet implement non-blocking ? It's simply called O_NONBLOCK. For some reason, SO_REUSEPORT was commented for linux, I uncommented it. Michael.
Giuliano Colla
to Michael Van Canneyt: They are not missing. How do you think fpsock and lnet implement non-blocking ? It's…

Il 19/05/2020 18:57, Michael Van Canneyt ha scritto:

They are not missing. How do you think fpsock and lnet implement non-blocking ? It's simply called  O_NONBLOCK.

I'm using fpc 3.0.4. With just Sockets in the uses clause both SOCK_NONBLOCK and O_NONBLOCK give an Identifier not found error. I've seen that SO_REUSEPORT was commented out, so I just defined it in my code.

Do not do to others as you would have them do to you.They might have different tastes.
Norbert Saint Georges
to Giuliano Colla
Giuliano Colla wrote 11 lines
Il 19/05/2020 18:57, Michael Van Canneyt ha scritto: > They are not missing. > > How do you think fpsock and lnet implement non-blocking ? > It's simply called  O_NONBLOCK. I'm using fpc 3.0.4. With just Sockets in the uses clause both SOCK_NONBLOCK and O_NONBLOCK give an Identifier not found error. I've seen that SO_REUSEPORT was commented out, so I just defined it in my code.
With lnet I use:
fsock.SetState (ssBlocking, false);
fsock.SetState (ssReuseAddress, true);
Norbert Saint Georges http://tetrasys.fi
Michael Van Canneyt
to Giuliano Colla: Il 19/05/2020 18:57, Michael Van Canneyt ha scritto: I'm using fpc 3.0.4. With just…
Giuliano Colla wrote 11 lines
Il 19/05/2020 18:57, Michael Van Canneyt ha scritto: > They are not missing. > > How do you think fpsock and lnet implement non-blocking ? > It's simply called  O_NONBLOCK. I'm using fpc 3.0.4. With just Sockets in the uses clause both SOCK_NONBLOCK and O_NONBLOCK give an Identifier not found error. I've seen that SO_REUSEPORT was commented out, so I just defined it in my code.

You'll need the unix/baseunix unit for O_NONBLOCK.

I believe on windows there is a similar define, but I'm not sure where. It must exist, because the non-blocking works cross-platform.

Michaem.