Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Contributor
Semaphor
Posts: 11
Registered: ‎05-07-2013
My Device: Z10
My Carrier: Telus
Accepted Solution

Hosting sockets (server) on BB10 does not work. Advice?

[ Edited ]

I saw in the app store an FTP server application, so I tried it. It hosts an open port that I can connect to with my FTP client, which is great. However, when my own application (which is a native cascades app) tries to host an open port, I cannot connect to it. I get a 'connection denied' on the client. During my connection attempts, my application (who was accepting connections and being debugged) did not unblock from the listening state because it never recieved the connection. This situation is evident whether or not I'm debugging or just running the application by launching it manually.

 

I can only conclude that there is a firewall of some sort on the device, which blocks incoming connections (and thus, hosting sockets). Or routing of open sockets is somehow disabled.

 

My question is: why are android applications allowed to host a socket srever while cascades applications are not? And more importantly, is there a workaround??

 

EDIT: I do have the acccess_internet permission enabled.

Please use plain text.
Developer
mreed
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Hosting sockets (server) on BB10 does not work. Advice?

You might want to post some code of what you're testing with.

Please use plain text.
Contributor
Semaphor
Posts: 11
Registered: ‎05-07-2013
My Device: Z10
My Carrier: Telus

Re: Hosting sockets (server) on BB10 does not work. Advice?

[ Edited ]

The code is fairly straight forward. The TcpServer listens and should call into the newConnection() slot:

 

QTcpServer _server;

// Hook up server events
QObject::connect( &_server, SIGNAL( newConnection() ), this, SLOT( onNewConnection() ) );

// ...

void Server::start()
{
    // Begin listening for connections
    if ( _server.listen( QHostAddress::Any, BIND_PORT ) ) //Port 5335
    {
        _server.setMaxPendingConnections( LISTEN_BACKLOG ); //backlog of 32
    }

    while ( _server.isListening() ) // Server constantly waits
    {
        // If a new connection is available, onNewConnection() will be triggered
        _server.waitForNewConnection( -1 );
    }
}

void Server::onNewConnection()
{
    // NOTE: This never gets called
}

 

On the client side, I have the following code:

 

QTcpClient _sock;

// connect to all socket signals. error() and disconected() get called stating 'Connection Refused'
// ...

void Client::connect()
{
    // If the socket is connected, close it
    if ( _sock.isOpen() )
    {
        _sock.abort();
    }

    // Begin connect
    _sock.connectToHost( hostIP, BIND_PORT ); // Port 5335
}

 

I even tried this using standard BSD sockets. I get the same result.

Please use plain text.
Developer
mreed
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Hosting sockets (server) on BB10 does not work. Advice?

Just to get it out of the way... you're testing this on WiFi, right?

Please use plain text.
Contributor
Semaphor
Posts: 11
Registered: ‎05-07-2013
My Device: Z10
My Carrier: Telus

Re: Hosting sockets (server) on BB10 does not work. Advice?

Yes I am. The FTP application I used was also tested on Wifi and it works fine.
Please use plain text.
Developer
Zmey
Posts: 1,512
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: Hosting sockets (server) on BB10 does not work. Advice?

Hi,

 

I've just created a test project and can confirm that QTcpServer works using WiFi on Dev Alpha A:

 

Device logs:

May 08 15:58:04.371 Server started 

May 08 15:58:55.953 New connection 

 

$ telnet 192.168.0.33 5335
Trying 192.168.0.33...
Connected to 192.168.0.33.
Escape character is '^]'.

 

This part of code is not needed. It is blocking the event loop and the slot is not called:

while ( _server.isListening() ) // Server constantly waits
    {
        // If a new connection is available, onNewConnection() will be triggered
        _server.waitForNewConnection( -1 );
    }

 

Here is my code:

 

TestProj.hpp

 

#ifndef TestProj_HPP_
#define TestProj_HPP_

#include <QObject>
#include <QTcpServer>

namespace bb { namespace cascades { class Application; }}

class TestProj : public QObject
{
    Q_OBJECT
public:
    TestProj(bb::cascades::Application *app);
    virtual ~TestProj() {}

protected slots:
    void onNewConnection();

protected:
    QTcpServer *tcpServer_;
};


#endif /* TestProj_HPP_ */

TestProj.cpp:

 

#include "TestProj.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>

using namespace bb::cascades;

TestProj::TestProj(bb::cascades::Application *app)
: QObject(app)
{
    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

    AbstractPane *root = qml->createRootObject<AbstractPane>();
    app->setScene(root);

    tcpServer_ = new QTcpServer(this);
    QObject::connect(tcpServer_, SIGNAL(newConnection()), this, SLOT(onNewConnection()));

    if (!tcpServer_->listen(QHostAddress::Any, 5335))
    {
    	qDebug() << "Unable to start the server:" << tcpServer_->errorString();
    }
    else
    {
    	qDebug() << "Server started";
    }
}

void TestProj::onNewConnection()
{
	qDebug() << "New connection";
}

 

 

 

 

 

 

 


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Contributor
Semaphor
Posts: 11
Registered: ‎05-07-2013
My Device: Z10
My Carrier: Telus

Re: Hosting sockets (server) on BB10 does not work. Advice?

Looks like my while loop was the problem. Thanks!
Please use plain text.
Developer
monmomo04
Posts: 58
Registered: ‎05-06-2012
My Device: Blackberry torch 9850
My Carrier: Voila

Re: Hosting sockets (server) on BB10 does not work. Advice?

[ Edited ]

Does it work on the simulator ?

Please use plain text.