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. Problem in client - server model of tcp in qt
Forum Update on Monday, May 27th 2025

Problem in client - server model of tcp in qt

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 3.1k Views
  • 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.
  • S Offline
    S Offline
    shakthi
    wrote on last edited by
    #1

    hi,

    i like to implement simple client -server model of qt. but unfortunately i dint get proper output. its totally runtime error. please help me to find out..

    here my server code which will receive the output once connected with client :

    code:

    @
    #include "widget.h"
    #include "ui_widget.h"
    #include "QtNetwork/qabstractsocket.h"
    #include "QtNetwork/qtcpserver.h"
    #include "QtNetwork/qtcpsocket.h"
    #include "QtNetwork/QHostAddress"
    #include "QMessageBox"
    #include "QDebug"
    #include "QDataStream"
    #include "stdio.h"
    Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
    {
    ui->setupUi(this);
    int s;
    serversocket=new QTcpServer();

    client=new QTcpSocket();
    serversocket->listen(QHostAddress::Any,5639);

    QObject::connect(serversocket,SIGNAL(newConnection()),this,SLOT(memberofserver()));
    

    }

    Widget::~Widget()
    {
    delete ui;
    }

    void Widget::memberofserver()
    {

        QTcpSocket *client = serversocket->nextPendingConnection();
        QObject::connect(client,SIGNAL(disconnected()),client,SLOT(deleteLater()));
        QDataStream in(client);
        in.setVersion(QDataStream::Qt_4_0);
        QString data;
        in >> data;
        ui->text1->setText(data);
    

    }
    @

    here my client code which will send data to server once it got connected :

    Code :

    @
    #include "widget.h"
    #include "ui_widget.h"
    #include "QMessageBox"
    #include "QAbstractSocket"
    #include "QTcpSocket"

    Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
    {

    ui->setupUi(this);
    
    client1=new QTcpSocket(this);
    client1->connectToHost("ipaddr...",5639);
    if(client1->state()==QAbstractSocket::ConnectedState)
        QMessageBox::information(this,tr("client"),tr("successfullyconnected"));
    else
        QMessageBox::information(this,tr("server"),tr("unsuccessfulcompletion"));
    
           client1->write("heysuper",20);
    

    }

    Widget::~Widget()
    {
    delete ui;
    }
    @

    please help me to find out soon

    thanks in advance,
    shakthi

    EDIT: please use @-tags for code highlight, Gerolf

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      Did you try to use the "fortune server / client example ":http://doc.qt.nokia.com/4.7/network-fortuneserver.html
      This may provide a better understanding for you how to do the server / client applications.

      BTW: You should start the code section with '@' and close it with the character. Otherwise it looks a bit messy.

      "Here is some helpful explanation on writing and formatting posts":http://developer.qt.nokia.com/wiki/ForumHelp

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • S Offline
        S Offline
        shakthi
        wrote on last edited by
        #3

        hi thanks for your reply.. i ve verified fortune server.. but pls tell me the error of my code.. sorry for the unformatted code..
        expecting reply from u?

        thanks in advance,
        shakthi

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mlong
          wrote on last edited by
          #4

          In your code the connection state is queried immediately after the call to connectToHost() is made, which more than likely doesn't give the connection time to complete. (It's not a blocking call.)

          You probably either want to call "waitForConnected()":http://doc.qt.nokia.com/4.7/qabstractsocket.html#waitForConnected or connect to the connected()/error() signals.

          Software Engineer
          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

          1 Reply Last reply
          0

          • Login

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