Results 1 to 2 of 2

Thread: problem in client and server model of tcp in qt

  1. #1
    Join Date
    Jul 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation problem in client and server model of tcp in qt

    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()),cli ent,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("suc cessfullyconnected"));
    else
    QMessageBox::information(this,tr("server"),tr("uns uccessfulcompletion"));

    client1->write("heysuper",20);
    }

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



    please help me to find out soon

    thanks in advance,
    shakthi

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: problem in client and server model of tcp in qt

    Qt Code:
    1. //In server
    2. serversocket = new QTcpServer(this); // set a parent to server (good practice)
    3. //client = new QTcpSocket(); //memory leak
    4.  
    5. //In client
    6. if(client1->state() == QAbstractSocket::ConnectedState)
    7. //you cannot expect to establish connection immediatly, either use waitForConnected() to wait or connect to connected() signal and then write data
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by shakthi
    but unfortunately i dint get proper output. its totally runtime error
    Try to be more specific, only then some can effectively help you out

Similar Threads

  1. problem with client/server application
    By marco.stanzani in forum Newbie
    Replies: 1
    Last Post: 9th April 2011, 07:36
  2. server-client problem
    By sksingh73 in forum Newbie
    Replies: 2
    Last Post: 3rd July 2010, 07:18
  3. TCP server-client app problem
    By pogostick in forum Newbie
    Replies: 6
    Last Post: 25th January 2010, 08:13
  4. Qt-- Client Server communication problem
    By thisismyuname in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2010, 01:04
  5. password problem with client and server
    By mate in forum Qt Programming
    Replies: 1
    Last Post: 19th July 2008, 18:20

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.