0

REQUIREMENT

A Client Server Application

Communication will be done by thrift

Server will be running in background or invoked through terminal with no GUI

Client will be Qt based


Current Scenario and problem

Currently, the server uses TNonBlockingServer with certain number of threads(using threadmanager)

Clients connect to the server and does the job.

But, there is a certain requirement where if my server is not running and client tries to connect to it then a message box should be displayed in client's screen.

Currently program just gives a segmentation fault, so i tried using try catch, which didn't work. Upon searching i noticed that Qt doesn't support Exceptions.

Upon some more searching i came across This, but this seems to be using QT(and I still don't know if my problem can be resolved by this)

Server Code :

shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
shared_ptr<workerHandlerHandler> handler(new workerHandlerHandler());
shared_ptr<TProcessor> processor(new workerHandlerProcessor(handler));
shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(15);
shared_ptr<PosixThreadFactory> threadFactory = shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
threadManager->threadFactory(threadFactory);
threadManager->start();

TNonblockingServer server(processor, protocolFactory, port,threadManager);
server.serve();

Client connects using

boost::shared_ptr<TSocket> socket(new TSocket(serverip.toUtf8().data(), 59999));
boost::shared_ptr<TTransport> transport(new TFramedTransport(socket));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
workerHandlerClient client(protocol);
transport->open();
int pingValue = client.ping();
transport->close();
Community
  • 1
  • 1
Ayush choubey
  • 606
  • 6
  • 23
  • A segfault indicates that there is some invalid pointer access. Where does that happen, and why can't you check for that condition (e.g. NULL pointer)? Having the whole error message would be a good thing ... – JensG Aug 04 '14 at 15:15
  • Since you don't answer questions and seem to have the problem solved: Is there anything wrong with the answers given? – JensG Aug 07 '14 at 21:02
  • 1
    Well, the answer given actually uses QT classes(QTcpSocket) to connect to server, Now as i am using thrift, I cant actually use those classes to do a RPC call. Regarding the question, it causes TThriftException, which if i use, then this problem occurs. However as the current workaround I am using std::Exception to prevent program from crashing. But regarding that TThriftException, I am still looking for the way to catch it – Ayush choubey Aug 08 '14 at 18:11

3 Answers3

0

Why not use something like:

QTcpSocket *socket = new QTcpSocket();
socket->connectToHost(serverip, serverport);
if(!socket->waitForConnected(time-in-msecs))
  QMessageBox::critical(some-ui-window-as-parent, "Error Title", "Error connecting Text");
Sebastian Lange
  • 3,879
  • 1
  • 19
  • 38
0

If the client is Qt based, you can use QTcpSocket to connect the server :

QTcpSocket clientSocket;

By connecting the error signal of the client socket to a slot, you can display appropriate message box when an error occurs. If the server is not running and the client tries to connect, the slot is called and a message box containing the error is shown :

connect( &clientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
        this, SLOT(tcpError(QAbstractSocket::SocketError)) );

clientSocket.connectToHost(ipAddress, portNo );

The slot is as :

void MyClass::tcpError(QAbstractSocket::SocketError error)
{

    QMessageBox::warning( this, tr("Error"),tr("TCP error: %1").arg( clientSocket.errorString() ) );

    clientSocket.disconnectFromHost();

}
Nejat
  • 31,784
  • 12
  • 106
  • 138
-1

I don't know what you really mean.Qt just support try and catch. Now, I'm developing a program which is similar with your program. I also use No-QT server and QT client, but in my code I use the struct like below:

try
{
}
catch(TException e)
{
    QMessageBox::information(this, "Warn", "Thrift server has shut down");
}

And it works well

Ericwang
  • 11
  • 3
  • Why someone thinks my answer is wrong. I test it and it works very well. I really cannot understand that. – Ericwang Nov 22 '14 at 12:29