C++ Client Chatting

With the new standards in C++11 with std::thread, I was wondering if there is a way to make a pure C++ client to client chat that uses sockets. I have limited experience with sockets since I had no idea how to send a message without forcing the program to wait for a reply from the server first. I don't see why a server is required if you are directly connected with just one other computer, and especially now that threads are standard.

To make my problem a little more understandable, is there a way to use win sockets to talk with another client like a typical instant messaging program? The tutorials on threading are very vague since its so new, and what I've learned from win sockets was prior the C++11 release so it either used non standard libraries, or it forced the user to wait for the other client to respond. I have also only seen client/server implementations for chat programs, why?
That's just the way sockets work. Clients initiate communication and servers listen for incoming connections. That's the definition of "client" and "server" in sockets programming. It wouldn't make sense to have a conversation where both participants are waiting for the other person to speak (both are servers), or both are speaking constantly without caring whether someone is paying attention (both are clients).

is there a way to use win sockets to talk with another client like a typical instant messaging program?
AFAIK, when you need full duplex data transmission -- that is, both participants may send or receive data at any time, independently of each other -- what you do is create two connections where both programs are both client and server to each other.
Makes more sense when you put it in writing. I was thinking as a client listens and sends and a server just listens to clients and sends to clients. I was thinking of them as being physical objects. And now that both would have to have a client and server to handle incoming and out coming communications, how would threads play a role into this? As I've said, the thread tutorials are very few, and usually in a very technical manor for those that understand C++ just as good, if not better, than English.

From what I understand is that without using threads, you can either use a listening socket, or a sending socket, but can't use both at the same time. Maybe I'm wrong? How would you be able to handle this in an efficient way so that the program doesn't hang while waiting for the other client, or doesn't just repeatedly ask for messages without displaying the other clients messages?
And now that both would have to have a client and server to handle incoming and out coming communications, how would threads play a role into this?
Each program runs a client thread and a server thread. The server thread usually just idles while it waits for the other program to send data.
The exact form the client thread takes can vary a lot, depending on the design of the program. For example, it can idle while it waits for a thread-safe queue to be filled by another thread and then sends the data over the socket, or it can be the main thread and avoid synchronization data structures. Which is best will depend on your requirements.
You could also mention what GUI are you planing to use. Because if it is console application you would need to use something like allegro.h to have the ability to receive message while writing one.

In visual application such problem practically doesn't exist.

Honestly I have been wondering about client-to-client communication with only one socket myself :). Let's assume we have 2 threats:

1 threat - rcv
1
2
3
4
5
6
7
int iRcvLen=0;

while(1)
{
iRcvLen=recv(...);
if (ret >0) print_message;
}


2nd threat - send

1
2
3
4
5
6
while( fgets(...) )
{
if () <- check if 1st threat is busy, if YES - stop receiving
sendto(...);
//start receiving again
}


But such solution would have a lot of sync problem?... I will try it after work :).
Last edited on
@helios
My issue is that I don't know how to begin to even use the new std thread. Maybe I'm just making it seem more complicated than it needs to be, but I haven't found much information on using the new standard. Is there any good reference material? The concept is easy enough for me, but I can't apply it to anything since I'm unsure about how to use the syntax properly.

@tath
I'm programming for windows at the moment, but I don't want to use allegro, I want to learn to use standard libraries. I want to learn using a console application first, then work towards making it in my windows GUI. I am trying to stay away from third party libraries at the moment, and now that threads are standard, I figured it would be best to learn that.
My issue is that I don't know how to begin to even use the new std thread. ... I am trying to stay away from third party libraries at the moment, and now that threads are standard, I figured it would be best to learn that.

Does your implementation support std thread? If not, you'll need to use something else.
In theory, I can easily convert my program to handle threads. But again, I don't know since I'm unsure on how to use them. As for my compiler, I'm using the latest MinGW GCC which I believe has thread support, but since I've been unable to find a complete example of using threads, I can't test if it supports the new standard. I do know that it supports the std::function standard, I've already written my program to include it.
pure C++ client to client chat that uses sockets

You'd have to wait a few more years for boost.asio to become officially a part of the C++ standard. Until then, sockets aren't "pure".
That was something that I was aware of, but it's what I can use at the time. I've been hearing a lot about the boost Asio library and I've been looking into it, but I haven't wanted to use a library unless I get dead ended on C++. The windsock tutorials I've read have been fairly straight forward since I have a networking background, but the code for the sockets isn't as easy as I thought it was going to be. Asio also supports threading if I'm correct and is portable as well, no?
Asio also supports threading if I'm correct

yes, in most common use case, you launch some threads and hand them off to asio so it can run stuff asynchronously.

and is portable as well, no?

yes, it's exactly the same on Windows, Linux, BSD, and elsewhere
I have been reading for the past several hours and maybe I'm just overloading my brain, but is there a good tutorial on threads? Basically from what I've been reading is that when you create a thread, it does it's job, which is perfect for sockets, but unless you join the thread, main continues unhindered, correct? I haven't done research into Asio yet, but boost::thread and std::thread have been looking eerily familiar. Would it be best to just learn the boost implementation? It seems more complicated, but also seems to have much more support than C++11 features do.
is there a good tutorial on threads?

A tutorial would be insufficient. There are good books on threads. If you're looking at boost/C++, then get C++ Concurrency in Action.

boost::thread and std::thread have been looking eerily familiar

boost is the test platform for C++ extensions. After several years of successful widespread use, boost::threads became part of C++, with a few minor changes, which were backported into boost afterwards.

Would it be best to just learn the boost implementation?

It would make no difference.
Topic archived. No new replies allowed.