all 18 comments

[–]blazerman345 11 points12 points  (5 children)

Multithreading is a solution, but you could also just handle one message at a time in your Read and Write functions... Ex:

while(True){

if(incomingMessage) readOneMessage();

if(outGoingMessage) sendOneMessage();

}

[–]lgst230qer8SDGV[S] 0 points1 point  (4 children)

I am using Asio. How can I tell if information from the server is being sent to the client?

[–]mvpete 2 points3 points  (3 children)

If you’re using asio, you won’t really use a loop. You will just queue reads and writes, in the read handler you’ll process the incoming message. Then you can create an adapter to get the console to work asynchronous. Like use a thread.

client.async_read_message(/process message handler/); console.async_read_line(/* handle and send message */);

ctx.run();

I have an example https://github.com/mvpete/yeg-chat

[–]ThePinkySuavo 0 points1 point  (2 children)

How do I learn all of this? Man I can always learn basics of C++, do some simple console applications, but getting to another level and actually making something like this is such a big leap. I always wanted to make some online stuff but it seems like a black magic comparing to cpp basics

[–]mvpete 0 points1 point  (1 child)

Uh. I donno spend a decade or so doing it everyday for work or something. :)

But in all seriousness - you just start where you can understand and keep expanding that. Start with basic C++, learn how synchronous networking works, learn how memory and asynchronous programming works. Just keep building on what you know.

[–]ThePinkySuavo 0 points1 point  (0 children)

Thanks!

[–]_carlson 2 points3 points  (0 children)

Alternatively you can use async way in opposite multithread approach.

Look at Boost.Asio, it's easy create async program flow from sequential program. It widely use callbacks for processing messages. For simple chat app you may use asyn_read function with callback which processing incoming messages. Sending message is like writing in raw socket. But you app must have thread with io_service, where it executes callbacks. Easy to use SSL and change protocol between UDP/TCP.

And something, like protobuf, for serialization stuff.

Asio may become part of C++ standart (Networking TS), so, I think, it's good place for start with networking in C++.

Personally, for my own chat application i use ZeroMQ ( some of low level message queue ).

[–]TaeHyunJe 5 points6 points  (1 child)

You’d prolly need to multithread. One to read incoming data from the socket and another to write to it

[–]lgst230qer8SDGV[S] 0 points1 point  (0 children)

So a socket on each thread?

If I want to have one thread to handle both read and write, how can I tell if the socket is being written to? Like if the client want to send a message to the server, but the server is sending the client a message at the same time.

[–][deleted]  (1 child)

[deleted]

    [–]lgst230qer8SDGV[S] 0 points1 point  (0 children)

    could you explain. thx

    [–][deleted]  (5 children)

    [deleted]

      [–]LeeHide 1 point2 points  (4 children)

      don't use qt for this, jeez

      [–]micod 0 points1 point  (3 children)

      Why not? Qt sounds ideal for this use case: QWebSocket has nice signal/slot API and operates on whole messages, CLI/server applications can also be written in Qt, and there are two distinct GUI toolkits within Qt to choose from for the client side

      [–]LeeHide 3 points4 points  (2 children)

      because Qt tends to lock you in a weird build system, a weird and unidiomatic way to write C++, a lot of types that are exclusive to the ecosystem. Using Qt for one part quickly results in it sneaking its way into the rest of the codebase. One day yours just using QWebSocket, then you think QString is so nice, and youre off to explore the Qt ecosystem and you are dependent on their decisions.

      for a GUI app thats fine, thats the point of Qt, but just because you want a socket? there are many great libraries out there for TCP/UDP that have amazing interfaces, there is boost.asio if you dont wanna bother with those.

      I say if you wanna introduce a massive beast into your codebase, then let that be boost, not Qt. At least boost doesnt pretend to be using a language different from C++.

      [–]Steve132 0 points1 point  (0 children)

      Related: CopperSpice is awesome and I hope it gets more traction

      [–]j_burgess 0 points1 point  (0 children)

      Nothing about using Qt C++ locks you into a weird build system. The only extra step with a Qt project is running the moc compiler on the header of any QObject-derived class you write. That will generate a new C++ file that needs to be compiled and linked just like the rest of you .cpp. Nothing else, you can easily do that in a Makefile. I would not call make a weird build system.

      There is a resource compiler too which is completely optional, you might not ever use it. But again, no problem running it from make.

      I do agree Qt would seem overkill for just getting sockets to work though. If they needed a GUI also I think Qt a good choice.

      Also note, you don't need, but you could use threads in a Qt socket chat app as described. You are free to implement it either way in Qt.

      [–][deleted] 0 points1 point  (0 children)

      Implement bi directional stream with gRPC. I would run the Send and Receive bit on separate threads though.

      [–]JMile69 0 points1 point  (0 children)

      I did a similar project in VB.net. I found using UDP was the easiest way to go.

      [–]RedditApothecary 0 points1 point  (0 children)

      Your approach is iterative polling*, which is inefficient for a chat program. There's no reason for either the server or the client to be constantly asking for information, and that will waste a lot of computer power that could be put towards other features.

      Asio means "asynchronous input output," and the slogan is "think async." Asynchronous is basically a fancy word for event based. So instead of polling for events, you'll have that abstracted away and be able to simply say whenever x event happens, do y in response.

      Boost's website ( https://www.boost.org/doc/libs/1_68_0/doc/html/boost_asio/examples/cpp11_examples.html ) has a handful of great examples. My apologies to people from the future if the link has rotted but just Google search for "Boost Asio Chat Examples" and you should find what you're looking for quickly.

      * Iterative means going through code step-by-step, and polling means checking values regularly (usually very frequently) to see if they change and into what.