Ustream client example?

is there better example how to use ustream with ssl?
I don't understand how to send messages to the server via ustream?

After the example_connect_ssl() call, the struct ustream_ssl ssl has been attached as I/O backend to struct ustream_fd stream.

You can from then on perform ustream read/write operations like ustream_write(), ustream_printf(), ustream_read() on &

If you're in an uloop callback you can obtain a handle to the stream using something like this:

static void uloop_read_cb(struct uloop_fd *ufd, unsigned int events)
    struct ustream_fd *usfd = container_of(ufd, struct uloop_fd, fd);
    struct ustream *us = &usfd->stream;

    size_t len;
    char buf[1024];

    len = ustream_read(us, buf, sizeof(buf));
    printf("Read %ld bytes from SSL connection: %s\n", len, buf);

As an alternative to the container_of() solution you can place a reference to the stream in a global variable, like the example code did with static struct ustream_fd stream;

1 Like

To write an SSL encoded message to the just connected socket, do something like this after example_connect_ssl() in line 107:

const char *msg = "This is a test";
ustream_write(&, msg, strlen(msg), false);

You're also supposed to re-register &stream.fd with uloop_fd_add(...) at this point to receive future read/write notifcations for handling with your own callbacks.

1 Like

I will immediately try this. xD

How can I allow self signed certificates?