Problems using Qt5 and Apache Thrift
PAULUS, Raimund, TI-ABN
Raimund.Paulus@dillinger.biz
Wed Mar 25 09:04:10 GMT 2020
Problems using Qt5 and Apache Thrift
For several months i use Apache Thrift 0.11.0 on Cygwin 2.5.2 for data transfer between Windows boxes and my Linux host. On the Windows boxes a C++-application serves as a client. The user makes some input on an ascii-interface (created with libwed) and sometimes data must be transferred between the pc and the Linux host.
Here is the program sequence on the windows client:
//------------------------------------------------------------------------------
program starts
step 1: make the connection to the Linux server (Apache Thrift)
step 2: user interface (ASCII)
step 3: data transfer PC <-> Linux-Host (Apache Thrift)
step 4: user interface (ASCII)
step 5: data transfer PC <-> Linux-Host (Apache Thrift)
...
...
...
step n: close the connection to the host (Apache Thrift)
program ends
//------------------------------------------------------------------------------
This works flawlessly.
Now i want to implement the interface parts with Qt 5. Here is the new program sequence:
//------------------------------------------------------------------------------
program starts
step 1: make the connection to the Linux server (Apache Thrift)
step 2: initialize Qt interface (create widgets, buttons, ...)
step 3: user interface (Qt)
step 4: data transfer PC <-> Linux-Host (Apache Thrift)
step 5: user interface (Qt)
step 6: data transfer PC <-> Linux-Host (Apache Thrift)
...
...
...
step n-1: end Qt app
step n: close the connection to the host (Apache Thrift)
program ends
//------------------------------------------------------------------------------
During step 2 the connection to the linux server is broken. You can see it with the netstat command. First error message arises in step 4:
"TSocket::write_partial() send() <Host: my_host Port: 9090>Broken pipe"
On a Linux box the client program runs perfectly.
On the windows box the program works, if i initalize Qt before the connection to the server is made (step 2 before step 1). But that is not acceptable for me, because afterwards other widgets and buttons are created and i can not close and create the connection at each point.
For the tests I used the examples from the Apache Thrift Tutorial. The service on the linux box is created from CppServer.cpp. The client in the original is CppClient.cpp. But I modified it for Qt. Here is the source for CppClient.cpp. The connection is broken after line 66.
1 #include <stdio.h>
2 #include <iostream>
3
4 #include <QApplication>
5 #include <QWidget>
6 #include <QString>
7 #include <QMessageBox>
8 #include <QPushButton>
9
10 #include <thrift/protocol/TBinaryProtocol.h>
11 #include <thrift/transport/TSocket.h>
12 #include <thrift/transport/TTransportUtils.h>
13 #include <thrift/stdcxx.h>
14
15 #include "../gen-cpp/Calculator.h"
16
17 using namespace std;
18 using namespace apache::thrift;
19 using namespace apache::thrift::protocol;
20 using namespace apache::thrift::transport;
21
22 using namespace tutorial;
23 using namespace shared;
24
25 // function for messagebox
26 void MsgBox(const char *msg)
27 {
28 QMessageBox box;
29 box.setIcon(QMessageBox::Information);
30 box.setText("Server Return");
31 box.setInformativeText(QString::fromStdString(msg));
32 box.setStandardButtons(QMessageBox::Ok);
33 box.exec();
34 }
35
36 int main(int argc, char **argv) {
37
38 //-------------------------------------------------------
39 // connect to host before initialising Qt
40 //-------------------------------------------------------
41 printf("Connect to host \"NAZV\"with port 9090\n");
42
43 // stdcxx::shared_ptr<TTransport> socket(new TSocket("localhost", 9090));
44 stdcxx::shared_ptr<TTransport> socket(new TSocket("NAZV", 9090));
45 stdcxx::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
46 stdcxx::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
47 CalculatorClient client(protocol);
48
49 try {
50 transport->open();
51
52 //------------------------------------------------------
53 // initialising Qt after connect to host
54 //------------------------------------------------------
55 printf("Stop 1; Press <Enter> to continue"); getchar();
56
57 QApplication app(argc, argv);
58 QString txt;
59
60 printf("Stop 2; Press <Enter> to continue"); getchar();
61
62 QWidget *w = new QWidget();
63
64 printf("Stop 3; Press <Enter> to continue"); getchar();
65
66 QPushButton *b_end = new QPushButton("End", w);
67
68 printf("Stop 4; Press <Enter> to continue"); getchar();
69
70 QObject::connect(b_end, SIGNAL(clicked()), &app, SLOT(quit()));
71
72 printf("Stop 5; Press <Enter> to continue"); getchar();
73
74 w->show();
75
76 printf("Stop 6; Press <Enter> to continue"); getchar();
77
78
79 client.ping();
80 MsgBox("ping()");
81
82 txt.clear();
83 txt.append("1 + 1 = ");
84 txt.append(QString::number(client.add(1, 1)));
85 MsgBox(txt.toStdString().c_str());
86
87 Work work;
88 work.op = Operation::DIVIDE;
89 work.num1 = 1;
90 work.num2 = 0;
91
92 try {
93 client.calculate(1, work);
94 MsgBox("Whoa? We can divide by zero!");
95 } catch (InvalidOperation& io) {
96 txt.clear();
97 txt.append("InvalidOperation: ");
98 txt.append(io.why.c_str());
99 MsgBox(txt.toStdString().c_str());
100 }
101
102 work.op = Operation::SUBTRACT;
103 work.num1 = 15;
104 work.num2 = 10;
105 int32_t diff = client.calculate(1, work);
106 txt.clear();
107 txt.append("15 - 10 = ");
108 txt.append(QString::number(diff));
109 MsgBox(txt.toStdString().c_str());
110
111
112 app.exec();
113
114 transport->close();
115 } catch (TException& tx) {
116 cout << "ERROR: " << tx.what();
117 }
118
119 }
The result is the same on Cygwin 3.1.4 and Apache Thrift 0.12.0.
I would be very grateful for your help
Raimund Paulus
More information about the Cygwin
mailing list