Problem in client - 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()),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,
shakthiEDIT: please use @-tags for code highlight, Gerolf
-
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
-
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 -
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.