Hi,
I'm a newbie in qt
I use nokia qt sdk, installed trk on the device and I have all the sdk up to date.
I have a problem, I 'm trying to send a simple hello world from a nokia 5800 XpressMusic to a simple application on the pc via QTcpSocket. When I try the to send the "hello world" by the simulator, it works fine, BUT when I deploy the application to the device I notice the QAbstractSocket::ConnectedState stay in the stateconnecting. I don't understand why? so follow the code I wrote:
Client
main.cpp:
mainwindow.h:Code:#include <QtGui/QApplication> #include "mainwindow.h" #include "client.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); Client c; //c.sendXML(); MainWindow w; #if defined(Q_WS_S60) w.showMaximized(); #else w.show(); #endif return a.exec(); }
mainwindow.cpp:Code:#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
client.h:Code:#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; }
client.cppCode:#ifndef CLIENT_H #define CLIENT_H #include <QObject> #include <QtNetwork> #include <QTcpSocket> #include <QHostAddress> class Client : public QObject { Q_OBJECT public: explicit Client(QObject *parent = 0); signals: public slots: private slots: void sendXML(); private: QTcpSocket* socket; }; #endif // CLIENT_H
ServerCode:#include "client.h" Client::Client(QObject *parent) : QObject(parent) { //set up connection socket = new QTcpSocket(this); socket->connectToHost(QHostAddress::QHostAddress("192.168.0.191"), 44444, QIODevice::WriteOnly ); connect(socket, SIGNAL(connected()), this, SLOT(sendXML())); if(socket->isOpen()) qDebug() << "is open"; } void Client::sendXML(){ QByteArray block = "hello World"; block.append("!!!!"); qDebug() << socket->state(); if(socket->state() != QAbstractSocket::ConnectedState){ qDebug() << "error:" << socket->errorString(); return; } if(socket->isValid()){ socket->write(block, block.length()); qDebug() << "sent!!!!"; } }
main.cpp
mainwindow.hCode:#include <QtGui/QApplication> #include "mainwindow.h" #include "server.h" #include <QSqlDatabase> int main(int argc, char *argv[]) { QApplication a(argc, argv); qDebug() << QSqlDatabase::drivers(); Server *s = new Server(); MainWindow w; w.setServer(s); s->setMW(&w); w.show(); return a.exec(); }
mainwindow.cppCode:#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include "server.h" namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); void setServer(Server *s); void setTextLabel(QString str); private: Ui::MainWindow *ui; Server *s; }; #endif // MAINWINDOW_H
server.hCode:#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); //s.setLabel(this->ui->label); } MainWindow::~MainWindow() { delete ui; } void MainWindow::setServer(Server *s) { this->s = s; } void MainWindow::setTextLabel(QString str){ this->ui->label->setText(str); }
server.cppCode:#ifndef SERVER_H #define SERVER_H #include <QObject> #include <QTcpServer> #include <QTcpSocket> #include <QLabel> class MainWindow; class Server : public QObject { Q_OBJECT public: explicit Server(QObject *parent = 0); void setMW(MainWindow *mw); signals: public slots: private slots: void on_newConnection(); void on_ReadyRead(); private: QTcpServer *server; QTcpSocket * s; MainWindow * mw; }; #endif // SERVER_H
thanks for helping!Code:#include "server.h" #include "mainwindow.h" Server::Server(QObject *parent) : QObject(parent) { // set up connection server = new QTcpServer(this); // if server does not listen if(! server->listen(QHostAddress::Any, 44444)){ server->close(); return; } // new connection coming in connect(server,SIGNAL(newConnection()),this, SLOT(on_newConnection())); } void Server::on_newConnection() { s = server->nextPendingConnection(); connect( s, SIGNAL(readyRead()), SLOT(on_ReadyRead()) ); } void Server::on_ReadyRead(){ QByteArray xml = s->readAll(); QString sXml(xml); qDebug() << "xml \n" << sXml; mw->setTextLabel(sXml); } void Server::setMW(MainWindow *mw){ this->mw = mw; }




