Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. [SOLVED]QTcpServer not listening on s60 Device
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]QTcpServer not listening on s60 Device

Scheduled Pinned Locked Moved Mobile and Embedded
11 Posts 3 Posters 7.4k Views 1 Watching
  • 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.
  • H Offline
    H Offline
    hasnain
    wrote on last edited by
    #1

    I have a simple application which use QTcpServer and QTcpSocket for accepting and making connections. the app is working fine on simulator server accept connections exchange data. But when i tested the app on device server listen on local host but fail to listen on WLAN ip. some one have any idea how i can make a server which listen on WLAN ip. Following is my .pro .h and .cpp files. Please Help

    .pro file
    @#-------------------------------------------------

    Project created by QtCreator 2010-11-26T15:34:52

    #-------------------------------------------------

    QT += core gui network xml

    TARGET = J2metest2
    TEMPLATE = app

    SOURCES += main.cpp
    mainwindow.cpp

    HEADERS += mainwindow.h

    FORMS += mainwindow.ui

    CONFIG += mobility
    MOBILITY =

    symbian {
    TARGET.UID3 = 0xe9a8f007
    TARGET.CAPABILITY += LocalServices NetworkServices
    TARGET.EPOCSTACKSIZE = 0x14000
    TARGET.EPOCHEAPSIZE = 0x020000 0x800000
    }
    @

    .h file
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include<QTcpServer>
    #include<QTcpSocket>
    #include<QNetworkInterface>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
    Ui::MainWindow *ui;
    QTcpServer server;
    QTcpSocket *serverSocket;
    QTcpSocket *clientSocket;
    void connectToServer();
    void startServer();
    private slots:
    void on_pushButton_2_clicked();
    void on_pushButton_3_clicked();
    void on_pushButton_clicked();
    void newConn();
    void serverSocketRead();
    void clientSocketRead();
    };

    #endif // MAINWINDOW_H
    @

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hasnain
      wrote on last edited by
      #2

      .cpp FIle
      @#include "mainwindow.h"
      #include "ui_mainwindow.h"

      MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
      {
      ui->setupUi(this);
      connect(&server,SIGNAL(newConnection()),this,SLOT(newConn()));
      // connect(clientSocket,SIGNAL(readyRead()),this,SLOT(clientSocketRead()));
      // connect(serverSocket,SIGNAL(readyRead()),this,SLOT(serverSocketRead()));

      }

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

      void MainWindow::startServer(){

          QString ownIpAddress;
          QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
          for (int i = 0; i < ipAddressesList.size(); ++i) {
              if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
                  ipAddressesList.at(i).toIPv4Address()) {
                  ownIpAddress = ipAddressesList.at(i).toString();
                  break;
              }
          }
          if (ownIpAddress.isEmpty())
              ownIpAddress = QHostAddress(QHostAddress::LocalHost).toString();
      if(!server.listen((QHostAddress)ownIpAddress,1235)){
              qDebug()<<"server can not be started";
      
          }
          else{
              qDebug()<<"Server started at"<<server.serverAddress()<<"Port"<<server.serverPort();
              ui->textBrowser->insertPlainText(tr("server started at : %1 and %2 ").arg(server.serverAddress().toString()).arg(server.serverPort()));
          }
      
      
      }
      

      void MainWindow::newConn(){
      char *data = new char();
      qDebug()<<"New Connection";
      ui->textBrowser->insertPlainText("New Connection");
      // qDebug()<<"threads "<<threads.count()<<" Textbrowsers "<<textBrowsers.count()<<"Sockets "<<connections.count();
      uint i;
      i=1024;
      // QString name;
      serverSocket =server.nextPendingConnection();
      connect(serverSocket,SIGNAL(readyRead()),this,SLOT(serverSocketRead()));
      QByteArray block;
      QDataStream out(&block,QIODevice::WriteOnly);
      // QString a=tr("(iq,bhatti");
      //// out<<a.toAscii();
      // serverSocket->write(a.toUtf8());
      // serverSocket->flush();

      }

      void MainWindow::connectToServer(){
      // char *data = new char();
      char *data = new char();
      uint i;
      i=1024;
      uint port;
      port = 1235;
      QTcpSocket *clientSocket = new QTcpSocket(this);
      connect(clientSocket,SIGNAL(readyRead()),this,SLOT(clientSocketRead()));
      clientSocket->connectToHost((QHostAddress)"192.168.10.119",port,QIODevice::ReadWrite);
      clientSocket->waitForConnected();
      if(clientSocket->state()== QAbstractSocket::ConnectedState){
      qDebug()<<"Connected to Server";
      ui->textBrowser->insertPlainText("Connected to Server");
      // clientSocket->waitForReadyRead();
      // clientSocket->readLine(data,i);
      // qDebug()<<data;
      // QString datain = data;
      }
      }
      void MainWindow::on_pushButton_clicked()
      {
      startServer();
      }

      void MainWindow::clientSocketRead(){
      char *data = new char();
      uint i;
      i=1024;
      QString a;
      clientSocket->readLine(data,i);
      a = tr("/n Other party Said;")+ data;
      ui->textBrowser->insertPlainText(a);
      }

      void MainWindow::serverSocketRead(){
      char *data = new char();
      QByteArray b;// = new QByteArray();
      QByteArray b2;
      uint i;
      i=1024;
      QString a;

      b = serverSocket->read(i);
      if(b.at(0)=='�'){
          b.remove(0,2);
          //b.append('�');
      }
      

      // QDataStream in(b2);
      // for(int k=0;k<b.length();k++){
      // *(data+k) = *(b+k);
      // }
      //data = b.data();
      // a.fromUtf8(data,20);
      //b.remove(0,2);
      //b.append('�');
      //serverSocin<<b;
      //a.fromUtf8(serverSocin);

      a = b;
      qDebug()<<a;
      

      // a = tr("/n Other party Said;")+ b.to
      ui->textBrowser->insertPlainText(a);
      }

      void MainWindow::on_pushButton_3_clicked()
      {
      // char *data = new char();
      uint i;
      i=1024;
      QByteArray b;
      // data = ui->lineEdit->text().;
      // if(clientSocket->state()== QAbstractSocket::ConnectedState){
      // clientSocket->write(ui->lineEdit->text().toLocal8Bit());
      // }
      if(serverSocket->state()== QAbstractSocket::ConnectedState){

          QDataStream out(&b,QIODevice::WriteOnly);
          out<<ui->lineEdit->text().toUtf8();
          serverSocket->write(b);
          serverSocket->flush();
      }
      

      }

      void MainWindow::on_pushButton_2_clicked()
      {
      connectToServer();
      }
      @

      1 Reply Last reply
      0
      • B Offline
        B Offline
        blex
        wrote on last edited by
        #3

        Is it possible to be an issue with firewall? Or some routing problem?


        Oleksiy Balabay

        1 Reply Last reply
        0
        • H Offline
          H Offline
          hasnain
          wrote on last edited by
          #4

          I have tried on device debugging the server error is "the address is not Available". where as app it self has extracted the WLAN from Network interface. The device I am using is Nokia 5800. Any idea how i can set firewall or permissions on device.

          1 Reply Last reply
          0
          • B Offline
            B Offline
            blex
            wrote on last edited by
            #5

            No idea about firewall on device, but may be it is network setting.

            I have the same device, so, if you send me the client and server sources then I may test on it tomorrow.

            Try to use

            @
            server.listen(QHostAddress::Any,1235)
            @


            Oleksiy Balabay

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tobias.hunger
              wrote on last edited by
              #6

              You do have the networking capability set, don't you?

              1 Reply Last reply
              0
              • B Offline
                B Offline
                blex
                wrote on last edited by
                #7

                [quote author="Tobias Hunger" date="1291017672"]You do have the networking capability set, don't you?
                [/quote]

                Please more details.


                Oleksiy Balabay

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  hasnain
                  wrote on last edited by
                  #8

                  Yes, i have capabilities set see .pro file
                  @
                  TARGET.CAPABILITY += LocalServices NetworkServices
                  @

                  and Yes i have tried
                  @server.listen(QHostAddress::Any,1235)@

                  server.serverAddress returned 0.0.0.0

                  Source of my app is as i have pasted above."blex" if you can just test @startServer() @ on your device and let me know that it works on WLAN ip.
                  the main.cpp for this app is as below
                  @#include <QtGui/QApplication>
                  #include "mainwindow.h"

                  int main(int argc, char *argv[])
                  {
                  QApplication a(argc, argv);
                  MainWindow w;
                  #if defined(Q_WS_S60)
                  w.showMaximized();
                  #else
                  w.show();
                  #endif

                  return a.exec&#40;&#41;;
                  

                  }@

                  And Thanks for taking interest in mu problem

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    blex
                    wrote on last edited by
                    #9

                    Return of 0.0.0.0 is normal, it means that connection may be established from any address.

                    I see that some characters are broken in the post, so, please, share the archive with sources.

                    Also, how should I check that it works? I need client to connect to the server.


                    Oleksiy Balabay

                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      hasnain
                      wrote on last edited by
                      #10

                      Problem Solved, there was nothing wrong in the code, it was issues with my WLAN i have manual ip for WLAN, which was not working for my app, i got mac of device bind with an ip in DHCP, now device get ip from DHCP and every thing is good.
                      Thanks 'Blex' for taking interest in my problem.

                      1 Reply Last reply
                      0
                      • B Offline
                        B Offline
                        blex
                        wrote on last edited by
                        #11

                        [quote author="hasnain" date="1291104380"]Problem Solved, there was nothing wrong in the code, it was issues with my WLAN[/quote]

                        Congratulations :)


                        Oleksiy Balabay

                        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