Understeaning Socket and chat basics

Hi,

I tried to create my little chat bash (following this tutorial for sockets : http://tldp.org/LDP/LG/issue74/tougher.html)

And i have a problem when I do my getline(cin, output) on a separated thread.



I can't find a complete article that explains how make a chat in c++ <u>on Linux</u>

So I made it following this logic :
When connection is established between server and client:
- launch a new thread with a getline(cin, output) to listen for keyboard input
- in main thread: Socket::read() waiting for incoming stream

Is it the best manner to do ?

I think that my problem (eated line) is caused by the multi-thread and the std:cin class

Here you have my source code (compiled on Ubuntu 16.04) :
https://mega.nz/#!mhA1xbaS!ebyf3ZhWuSs9T5Z_h7XGnmuJrysioNIrPBTHPPewZaM

Sorry for my bad English, I tried to do my best to explain myself.

To test iit: You have to launch 2 terminals. The first launches the server ("./simple_server") and then on the second, you can launch the client("./simple_client")

Thank you !
I have look at your code but have not had chance to test it. I can't see immediate error in your design, but as you point out that multi-threaded action can be tricky sometimes. For this type of reason I may suggest using select over set of sockets for read and write event which may help you soften multi-threaded action.

If however your solution permits then use one of the c++ frameworks. I would recommend using Kahless_9 framework KTransmissionMgr which will allow transmit a binary buffer to a destination (Ip, port) ad set callback for received transmissions which you be free to handle as wish.

The framework is for both Linux and windows but you should download the linux version either 64 bit or 32 from http://www.shankodev.com:9080/download

Once you download and install framework you can browse to your project/Tools/src/socket_tools/TxmMgrTool which is a utility that uses KTransmissionMgr to send and receive data. The utility has three classes TC_TxmBuff, TC_TxmFile and TC_TxmDual.
You will need to look at TC_TxmBuff which just transmits plain text buffers from one instance to other. The other test cases TC_TxmFile allows transmission of files or directory of files while TC_TxmDual allows for both buffer and file transmissions. All the source code for utilities will be there and should also give guidance to building own.

The learning curve for network application development with multi-threaded can be steep but if you want to get something to operate quickly in the real world production then use the Kahless_9 framework. Our company had bottle necks in network transmissions after profiling and managed to overcome using this framework for nearly a year now. But strictly speaking we can't just attribute success to use of KTransmissionMgr only because Kahless_9 has many threading abstractions we are also using.
You have a number of errors, some more significant than others.

First of all, a chat app should allow two clients to chat with each other. The two clients can be peers or clients of a common server.

A server typically runs without a terminal because it doesn't make since to have operator at the server. Thw setver runs autonomously.

The sockets library is notoriously difficult to wrap. Yours is OK as far as it goes, but it's not bug free.

Anyway, first things first. Fix the design.
Topic archived. No new replies allowed.