Easy sockets - don't exist yet?

Jacob Shtokolov jacob.100205 at gmail.com
Tue Dec 6 10:20:25 UTC 2022


On Saturday, 3 December 2022 at 11:08:53 UTC, Vincent wrote:
> Unfortunately even TECHNICALLY stream never was a "range"! It's 
> more like "queue of bytes", where you can never be sure you 
> even get these bytes.

A stream is exactly a range, a "range of ranges" if more 
specifically. All network devices work with chunks of data 
(buffers), either it's 1 byte or 4096 bytes in size.

So, such ranges may work either with strings (with "\n" 
terminator, like `byLine` or your `readLine` example), or a chunk 
of bytes (like `byChunk`).

---

As for your request about "Easy sockets": it seems there is 
nothing like that available for now in the standard library. 
Besides, your example with google is oversimplified: for example, 
there is no simple way to determine the transport layer protocol 
for a specific domain name and port:

```d
auto sock = new ClientSocket("google.com", 80);
sock.WriteLine("GET / HTTP/1.0");
```

You can technically guess between IPv4 and IPv6 by resolving the 
address via DNS, but you can't guess TCP vs UDP without trying to 
connect to a host. Moreover, there are other protocols like QUIC, 
which is built on top of UDP but has different semantics.

However, your demand is quite understandable. There is some 
ongoing work toward these ideas.

For example, what would you say about the following interface:

```d
auto s = socket("tcp://google.com:80");
s.connect();
s.writeln("Hello world");
s.close();
```

or, if asynchronous:

```d
auto t1 = socket("tcp://google.com:80")
   .connect()
   .writeln("GET / HTTP/1.0")
   .readln((sock, msg) => doSomething());

auto t2 = socket("tcp://duckduckgo.com:80")
   .connect()
   .writeln("GET / HTTP/1.0")
   .readln((sock, msg) => doSomethingElse());

wait(t1, t2);
```


More information about the Digitalmars-d-learn mailing list