Question #268


example request: TCP socket communication

Added by Eugen Wissner almost 2 years ago. Updated 2 months ago.

% Done:




Thank you for your library!

it would be nice to have some example of how to perform basic things: connecting, sending and receiving data, closing socket, etc.

WBR, basiliscos

belka-ew commented on 2018-06-10 at 17:29:15:

I originally implemented only server-side. The API is actually just an OOP wrapper over the libc socket API. Here is an example of a blocking server:

import std.stdio;
import tanya.memory;

void main()
    auto socket = defaultAllocator.make!StreamSocket(AddressFamily.inet);
    auto address = defaultAllocator.make!InternetAddress("",
                                                        cast(ushort) 8192);

    socket.setOption(SocketOptionLevel.SOCKET, SocketOption.REUSEADDR, true);
    socket.blocking = true;

    auto client = socket.accept();
    client.send(cast(const(ubyte)[]) "Test\n");

    ubyte[100] buf;
    auto response = client.receive(buf[]);

    writeln(cast(const(char)[]) buf[0 .. response]);


You can test it with telnet:

telnet 8192 

You should get "Test" printed in the telnet window. And you can print something in the telnet and it will be printed on the server side. After that the connection will be closed.

That said I finally started to work on a better networking capabilities with support for server and client without relying too much on libc (libc will be involved only for system calls).

basiliscos commented on 2018-06-12 at 15:59:30:
Thank you!

Yes, that would be nice to add socket-client communications into liblrary (+ example).

PS. I have noted that there is a @nogc format but I also wasn't able to figure out how to use it from the sources.

belka-ew commented on 2018-06-12 at 16:17:44:
Nice to see other people using the library :) I'll keep the issue open to track the development.

format is not finished yet. Unfortunately one thing is dead complicated in formatting: floating point conversion (even bigger problem is that the papers that describe fast algorithms are mostly not well written). I wouldn't like to make format public before I get floating point conversion right. I also want to go away from C-like formatting strings with %s, %i and so forth and use the syntax modern programming languages and use.

But you can convert individual values to strings, for example:

import std.stdio;
import tanya.container.string;
import tanya.conv;

void main()
    writeln(!String[]); // 8.5
    writeln(!String[]); // 9

Conversion itself is of course @nogc. If some type cannot be converted to string, it is already a bug, feel free to report. I'm working with tanya.container.String, not strings, because the latter is immutable(char)[] and it doesn't play nice with @nogc code.

belka-ew commented on 2018-06-12 at 16:25:14:
And the conversion is not only @nogc but also native, it doesn't use sprintf, so it is type safe.

I'll add the example above to the network docs and update format docs in the next days. Such issues are always useful, because I can see, where I wrote bad documentations. So thanks for reporting and further issues or questions are always welcome.

Actions #1

Updated by Eugen Wissner 2 months ago

  • Status changed from New to Closed

Also available in: Atom PDF