Project

General

Profile

Actions

Question #268

open

example request: TCP socket communication

Added by Eugen Wissner 11 months ago.

Status:
New
Priority:
Normal
% Done:

0%


Description

Hi,

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:
Hi,

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;
import tanya.network;

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

    socket.setOption(SocketOptionLevel.SOCKET, SocketOption.REUSEADDR, true);
    socket.blocking = true;
    socket.bind(address);
    socket.listen(5);

    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]);

    client.close();
    socket.close();
}

You can test it with telnet:

telnet 127.0.0.1 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 https://github.com/fmtlib/fmt use.

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

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

void main()
{
    writeln(8.5.to!String[]); // 8.5
    writeln(9.to!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.

No data to display

Actions

Also available in: Atom PDF