2

I'm fairly new to c++ and I'm wondering how can I use web sockets with it, I've used web sockets in NodeJs and JavaScript and I want to go on to use them with c++.

Jacob Owen
  • 81
  • 1
  • 1
  • 4
  • See https://stackoverflow.com/questions/6638213/will-and-should-there-be-sockets-in-c11. Sockets are not a part of the language itself, you need to use a third-party library, such as Boost or POSIX sockets. – sashoalm May 28 '16 at 12:06
  • 3
    You need to show some effort. What have you tried? What have you been researching? – Jesper Juhl May 28 '16 at 12:07
  • @JesperJuhl I've tried a few librarys but they dont work as expected. – Jacob Owen May 28 '16 at 12:09
  • 3
    Welcome to SO. Please review [MCVE]. In C++, and on Linux (and several embedded OS's), I have created both udp and stream sockets, always from scratch, I've not yet found a lib I like. To use Linux API, start with manuals (i.e. "man socket"). Also search SO, find articles. etc. – 2785528 May 28 '16 at 12:11
  • @Jacob Owen Then tell us what libraries you tried. What you expected and the results you got. Show some code. Don't expect people to just spoon-feed you solutions. Demonstrate to us that you have made an effort to solve the problem on your own and show us exactly what you are having problems with. Don't do this "How do I do X?" and "I tried something and it didn't work". That's not helpful and provides no real information. – Jesper Juhl May 28 '16 at 12:13

1 Answers1

7

Sockets are not part of the C++ standard library yet. But Boost has Boost.Asio, a cross platform library for talking TCP/IP and UDP among other things. There's this great open source library called Beast which handles not only WebSocket but HTTP as well, and its built on top of Boost.Asio. Here's the library home page: http://vinniefalco.github.io/

Here's a complete, compiling example program that talks WebSocket:

#include <beast/to_string.hpp>
#include <beast/websocket.hpp>
#include <boost/asio.hpp>
#include <iostream>
#include <string>

int main()
{
    // Normal boost::asio setup
    std::string const host = "echo.websocket.org";
    boost::asio::io_service ios;
    boost::asio::ip::tcp::resolver r(ios);
    boost::asio::ip::tcp::socket sock(ios);
    boost::asio::connect(sock,
        r.resolve(boost::asio::ip::tcp::resolver::query{host, "80"}));

    // WebSocket connect and send message using beast
    beast::websocket::stream<boost::asio::ip::tcp::socket&> ws(sock);
    ws.handshake(host, "/");
    ws.write(boost::asio::buffer("Hello, world!"));

    // Receive WebSocket message, print and close using beast
    beast::streambuf sb;
    beast::websocket::opcode op;
    ws.read(op, sb);
    ws.close(beast::websocket::close_code::normal);
    std::cout << to_string(sb.data()) << "\n";
}
Vinnie Falco
  • 5,173
  • 28
  • 43
  • Hey Vinnie, was just trying to test this code out and "to_string" doesn't seem to be in the latest repo on the github (I also separately downloaded boost a few days ago and can't seem to find it in there either).. There is however a "buffers_to_string" which is what "to_string" in your example seems to do - just wondering if this is the one I should be using? – orgg Jan 13 '21 at 16:59
  • 1
    use buffers_to_string – Vinnie Falco Jan 14 '21 at 18:10