Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. client in socketprogramming
Forum Updated to NodeBB v4.3 + New Features

client in socketprogramming

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 1.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JadeN001
    wrote on 7 May 2018, 09:15 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 7 May 2018, 09:21 last edited by
      #2

      Hi,

      Do you mean you would like to implement something like TBDChat ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply 7 May 2018, 10:02
      2
      • S SGaist
        7 May 2018, 09:21

        Hi,

        Do you mean you would like to implement something like TBDChat ?

        J Offline
        J Offline
        JadeN001
        wrote on 7 May 2018, 10:02 last edited by JadeN001 5 Jul 2018, 10:04
        #3

        @SGaist yes exactly using QTcpSocket.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 7 May 2018, 10:21 last edited by
          #4

          Well without any code it's pretty hard to even guess what you might be doing wrong.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 2 Replies Last reply 7 May 2018, 10:53
          0
          • S SGaist
            7 May 2018, 10:21

            Well without any code it's pretty hard to even guess what you might be doing wrong.

            J Offline
            J Offline
            JadeN001
            wrote on 7 May 2018, 10:53 last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • S SGaist
              7 May 2018, 10:21

              Well without any code it's pretty hard to even guess what you might be doing wrong.

              J Offline
              J Offline
              JadeN001
              wrote on 7 May 2018, 12:14 last edited by JadeN001 5 Jul 2018, 12:20
              #6

              @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);
              

              }

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 7 May 2018, 20:30 last edited by
                #7

                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 ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1

                1/7

                7 May 2018, 09:15

                • Login

                • Login or register to search.
                1 out of 7
                • First post
                  1/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved