×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    15

    Client on device connect to Server on pc by socket

    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:

    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.h:

    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
    mainwindow.cpp:

    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.h:

    Code:
    #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
    client.cpp

    Code:
    #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!!!!";
        }
    }
    Server

    main.cpp

    Code:
    #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.h

    Code:
    #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
    mainwindow.cpp

    Code:
    #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.h

    Code:
    #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
    server.cpp

    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;
    }
    thanks for helping!
    Last edited by Ludos; 2010-09-03 at 16:43.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    15

    Re: Client on device connect to Server on pc by socket

    Nobody know why is not working ?

    Ok i will ask by other way... How can I send data or connect to a remote sqlite by gprs or 3g?

    Thanks for helping!

  3. #3
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: Client on device connect to Server on pc by socket

    Do you have NetworkServices capabilities?

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    15

    Re: Client on device connect to Server on pc by socket

    Yes I think!

    following is my pro file

    Code:
    #-------------------------------------------------
    #
    # Project created by QtCreator 2010-09-01T09:27:16
    #
    #-------------------------------------------------
    
    QT       += core gui
    QT           += sql
    QT      += xml
    QT      += network
    
    TARGET = Servico+
    TEMPLATE = app
    
    
    SOURCES += main.cpp \
        blueprint.cpp \
        config.cpp \
        databasemanager.cpp \
        entity.cpp \
        room.cpp \
        unit.cpp \
        wall.cpp \
        frameblueprint.cpp \
        framecamera.cpp \
        framechooseclient.cpp \
        framelogin.cpp \
        framemeasure.cpp \
        frameorganize.cpp \
        frameroom.cpp \
        servicomais.cpp \
        databasemysql.cpp \
        customdoor.cpp \
        customgv.cpp
    
    HEADERS  += \
        blueprint.h \
        config.h \
        databasemanager.h \
        entity.h \
        room.h \
        unit.h \
        wall.h \
        frameblueprint.h \
        framecamera.h \
        framechooseclient.h \
        framelogin.h \
        framemeasure.h \
        frameorganize.h \
        frameroom.h \
        servicomais.h \
        databasemysql.h \
        customdoor.h \
        customgv.h
    
    FORMS    += \
        frameblueprint.ui \
        framecamera.ui \
        framechooseclient.ui \
        framelogin.ui \
        framemeasure.ui \
        frameorganize.ui \
        frameroom.ui \
        servicomais.ui
    
    CONFIG += mobility
    MOBILITY += multimedia bearer #sensors
    
    symbian {
        TARGET.UID3 = 0xec245946
        TARGET.CAPABILITY += UserEnvironment NetworkServices #ReadDeviceData
        TARGET.EPOCSTACKSIZE = 0x14000
        TARGET.EPOCHEAPSIZE = 0x020000 0x800000
    }
    
    RESOURCES += \
        Resources.qrc

Similar Threads

  1. Native Symbian Socket Server & J2ME Socket Client
    By romantr in forum Symbian Networking & Messaging (Closed)
    Replies: 1
    Last Post: 2010-04-21, 07:37
  2. create client socket to connect to server
    By amit.mangal in forum Symbian
    Replies: 1
    Last Post: 2009-09-30, 08:28
  3. Socket server in S60 2.0 emulator and socket client on Windows
    By arpit2agrawal in forum Symbian Networking & Messaging (Closed)
    Replies: 8
    Last Post: 2009-02-10, 22:17
  4. Problem in Sending data using Symbian Socket client to the PC based socket server
    By pankaj.dubey in forum Symbian Networking & Messaging (Closed)
    Replies: 1
    Last Post: 2008-02-26, 15:09
  5. SOCKET PROGRAMMING: Client can not connect to server...
    By simbiyan in forum Symbian Networking & Messaging (Closed)
    Replies: 5
    Last Post: 2007-04-22, 20:00

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×