API

To make your life simple, Crossroads are using standard POSIX socket API also known as BSD sockets. So, if you've done network programming before, you can start using Crossroads straight away.

There are few things to remember though:

  • API definition is located in xs.h header.
  • All the calls are prefixed by xs prefix (e.g. xs_socket, xs_send etc.)
  • To initialise the library use xs_init function, to terminate it use xs_term.
  • To bind and connect a socket, connection strings like tcp://10.0.0.2:5555 are used.

Here's an example of a simple server implemented in C. It can handle 10,000's of clients in parallel and replies "Hello, world!" to each message it gets from a client.

#include <xs.h>

int main ()
{
    void *ctx = xs_init (1);
    void *s = xs_socket (ctx, XS_REP);
    xs_bind (s, "tcp://*:5555");
    while (1) {
        char request [256];
        int nbytes = xs_recv (s, buff, 256, 0);
        xs_send (s, "Hello, world!", 13, 0);
    }
    xs_close (s);
    xs_term (ctx);
    return 0;
}

For more detailed desctiption of the API check the documentation section.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License