# How work with socket?

**URL:** <https://swi-prolog.discourse.group/t/how-work-with-socket/5450>\
**Category:** Discussion\
**Tags:** discussion\
**Created:** [June 2, 2022, 3:02am UTC](https://swi-prolog.discourse.group/t/how-work-with-socket/5450 "2022-06-02T03:02:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![heiheshang](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/heiheshang/32/4419_2.png) [@heiheshang](https://swi-prolog.discourse.group/u/heiheshang)\
**Post date:** [June 2, 2022, 3:02am UTC](https://swi-prolog.discourse.group/t/how-work-with-socket/5450/1 "2022-06-02T03:02:30Z")

</div>

I want to open connections to postgresql and write a message to a socket. I watch tcpdump nothing happens.

```prolog
tcp_address(Address0, Address),
    tcp_socket(Socket),
    tcp_connect(Socket, localhost:5432, Stream),
    set_stream(Stream, type(binary)),
    put_byte(Stream, 1),
    put_byte(Stream, 2),
    put_byte(Stream, 0)

```

How to correctly write a message to the server socket?

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [June 2, 2022, 4:33am UTC](https://swi-prolog.discourse.group/t/how-work-with-socket/5450/2 "2022-06-02T04:33:43Z")

</div>

Why are you doing things at this low level? Wouldn’t it be easier to use ODBC?  
[https://www.swi-prolog.org/pldoc/doc\_for?object=section(‘packages/odbc.html’)](https://www.swi-prolog.org/pldoc/doc_for?object=section(%27packages/odbc.html%27))

> [@SWI-Prolog connecting to PostgreSQL via ODBC](https://swi-prolog.discourse.group/t/swi-prolog-connecting-to-postgresql-via-odbc/2404):
>
> Note: This is a topic for learning how to connect SWI-Prolog to PostgreSQL via ODBC. If you have [Trust level](https://blog.discourse.org/2018/06/understanding-discourse-trust-levels/): [Basic](https://swi-prolog.discourse.group/badges/1/basic) you can edit this by clicking on the edit icon in the lower right. [Capture] Note: Do not reply to this topic; questions, concerns, comments, etc. are to be handled in [Wiki Discussion: SWI-Prolog connecting to PostgreSQL via ODBC](https://swi-prolog.discourse.group/t/wiki-discussion-swi-prolog-connecting-to-postgresql-via-odbc/2405) SWI-Prolog connecting to [PostgreSQL](https://www.postgresql.org/) via [ODBC](https://en.wikipedia.org/wiki/Open_Database_Connectivity) Ubuntu Installation Instructions Language agnostic steps 1. Install PostgreSQL (If not currently…

---

<div class="post-metadata">

**Author:** ![heiheshang](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/heiheshang/32/4419_2.png) [@heiheshang](https://swi-prolog.discourse.group/u/heiheshang)\
**Post date:** [June 2, 2022, 4:39am UTC](https://swi-prolog.discourse.group/t/how-work-with-socket/5450/3 "2022-06-02T04:39:11Z")

</div>

odbc is outdated and very slow, the wired protocol postgres provides more flexibility and speed.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [June 2, 2022, 7:13am UTC](https://swi-prolog.discourse.group/t/how-work-with-socket/5450/4 "2022-06-02T07:13:35Z")

</div>

> [@heiheshang](#):
>
> I watch tcpdump nothing happens

You need to flush the stream using flush\_output/1. B.t.w., there is no need to set the stream to binary. Socket streams start binary. Only for opening a file the default is to use text mode.

> [@heiheshang](#):
>
> odbc is outdated and very slow, the wired protocol postgres provides more flexibility and speed.

Did you actually measure that? It of course it depends on the driver, but my experience is not too bad. Anyway, you re not going to win using Prolog low-level I/O primitives. The Prolog overhead of byte-level I/O is quite surely going to be a lot higher than the ODBC overhead. Prolog is rather poorly equipped for low-level I/O. If you want full speed, write a wrapper around the native postgres client library in C or C++.

The point is, there are quite a few databases around and it is quite a bit of work to write a good and comprehensive database interface. Just check out the odbc wrapper sources. Probably the wrapper for an individual database is a little easier because the ODBC wrapper needs runtime quirks to deal with different features and limits provided by the various drivers, but still …

---

<div class="post-metadata">

**Author:** ![heiheshang](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/heiheshang/32/4419_2.png) [@heiheshang](https://swi-prolog.discourse.group/u/heiheshang)\
**Post date:** [June 3, 2022, 5:58am UTC](https://swi-prolog.discourse.group/t/how-work-with-socket/5450/5 "2022-06-03T05:58:40Z")

</div>

odbc is outdated, it seems to me that it is no longer worth using it now. I have experience using wired protocol libraries in other languages. I don’t want to pull dependencies into the project to connect with postgres.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [June 3, 2022, 7:34am UTC](https://swi-prolog.discourse.group/t/how-work-with-socket/5450/6 "2022-06-03T07:34:59Z")

</div>

It is all up to you 🙂 I just had a quick look at [PostgreSQL: Documentation: 14: Chapter&nbsp;34.&nbsp;libpq — C Library](https://www.postgresql.org/docs/current/libpq.html). It doesn’t look all that different from interfacing to ODBC. This implies that a comprehensive Postgress Prolog client library is roughly as complicated as the ODBC client ☹ Talking to the wire instead of this library is unlikely to make this any better. That said, if you want only a tiny bit of the functionality (which is not unlikely for a specific application) a direct binding might be worth considering. I’d first do some measurements, for example time Prolog ODBC against Python native binding (if it has one) for the things you want to do. Profiling the Prolog ODBC may also give a rough indication how much you can gain (for example using valgrind, so no recompilation is needed).

Please share or findings.
