client in socketprogramming
-
i want to create clientside program with out using GUI, in which it can chat with other clients.client can read or write data to another client at same time.I mean reading does not wait for writing and vice-versa.can any one suggest me how it is possible?
i have tried using signal and slot but once slot in writing mode until it complete its work the client can not read data sended by another client.
i am doing program in console programming. -
Well without any code it's pretty hard to even guess what you might be doing wrong.
-
@SGaist here is my client code.Before i was using QConcurrent thread to work read and write parallely but i was getting qsocketnotifier error so , to avoid qsocketnotifer error i have used movetothread concept in my code.here in this code after first time reading .i have to write somthing to get any further data to read.Because the reason is that i have emmited userinput() signal in readdata() slot if bytes not available(it means when completion of reading process).my question is how should i call getinput slots to get read and write data parallely.
client.cpp
void client::Start(){
m_socket=new QTcpSocket(this);QThread *T=new QThread(); m_socket->connectToHost("127.0.0.1",100); receiver *r=new receiver(m_socket); qDebug()<<"socket in socketStarter : "<<m_socket; qDebug()<<"thread in socketStarter : "<<QThread::currentThread(); r->moveToThread(T); connect(m_socket,SIGNAL(connected()),this,SLOT(connectionstarted())); connect(m_socket,SIGNAL(readyRead()),r,SLOT(readdata()),Qt::DirectConnection); connect(this,SIGNAL(sendtext(QTcpSocket*,QString)),this, SLOT(senddata(QTcpSocket*,QString))); connect(r,SIGNAL(userInput()),this,SLOT(getInput())); T->start();
}
void client::connectionstarted(){
qDebug()<<"connection successfully..";
}void client::senddata(QTcpSocket *s, QString text){
if(s->write(text.toStdString(). c_str())){ s->flush(); qDebug()<<" threadin write() is : "<<QThread::currentThread(); }
}
void client::getInput(){
QString text;
text=cin.readLine();
emit sendtext(m_socket,text);
}
receiver.cpp
receiver::receiver(QTcpSocket *s,QObject *parent):QObject (parent)
{
socket=s;
}void receiver::readdata(){
qDebug()<<"socket of receiveData:"<<socket; qDebug()<<"thread of receivedata slot : "<<QThread::currentThread(); do{ socket->waitForReadyRead(3000); if(socket->bytesAvailable()){ qDebug()<<"Message:"<<socket->readAll(); } if(!socket->bytesAvailable()){ emit userInput(); } }while(true);
}
-
Why are you moving your class to another thread if you then are forcing a direct connection ?
Also, why are you using an infinite loop using the blocking API ?