Qt Server Client Code - c++

I am a new to the QT programming. my server/client codes are quite simple but they are not working.......
pls have a look to find problems in my codes, thanks.
SERVER:
int main(int argc, char** argv)
{
// QApplication app(argc, argv);
// Server server;
QTcpSocket *client_sock = NULL;
QTcpServer server;
server.listen(QHostAddress::Any,8888);
char buff[100];
while(1)
{
if(server.hasPendingConnections())
{
client_sock = server.nextPendingConnection();
}
if(client_sock)
{
qint64 n_rtn;
n_rtn = client_sock->bytesAvailable();
client_sock->readLine(buff,n_rtn);
std::cout<<buff;
}
}
// return app.exec();
}
CLIENT:
int main(int argc, char** argv)
{
// QApplication app(argc, argv);
QTcpSocket client;
QHostAddress addr("127.0.0.1");
client.connectToHost(addr,8888);
if(client.isWritable())
{
client.write("Hello World!\n");
}
client.close();
// return app.exec();
}
Thanks

Without a QApplication or a QCoreApplication and an app.exec() nothing will work. This is what runs the event loop which handles all the keyboard/mouse/network events.
Take a look at the chat and fortune cookie network server examples to see how to do this - it's almost as simple as the code you have written

Related

Qt , how to create QApplication without argc and argv

Hey so I need to export a qt application as a .dll and , so I dont want any arguments like argc and argv , but QApplication needs them , so i tried this
int main()
{
int c=1;
char** v = (char**)("ApplicationName");
QApplication app(c,v);
MainWindow window;
window.show();
return app.exec();
}
but I get segfault from QtCore... Can someone help me bypass the segfault and create the QApplication without needing argc and argv?
This didnt solve the problem cause it needs argv defined ...
QApplication app(argc, argv)
Try this:
int main()
{
char* args[] = { (char*)"AppName" };
QApplication app(1,args);
MainWindow window;
window.show();
return app.exec();
}

Setting up server for other people to connect using Qt

I have a very small and simple server based on QTcpServer which just listens for new connections and once a new connection is connected it shows message about it.
Respectively, the same thing with my client. So, if i give my client app to my friend how can he connect to my seerver??? I have tested my server and client on locallhost (127.0.0.1) but never tried in the "real" world??? Can you please give me some hints???
//**SERVER**
#include <QTcpServer>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTcpServer server; //used 127.0.0.1 for localhost, but...
server.listen(???, 12345);//--->what to do to connect to the internet
return a.exec();
}
How can my friend connect to my server?
//**Client**
#include <QTcpSocket>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTcpSocket client;
client.connectToHost(???);
return a.exec();
}

QApplication Execution Segmentation Fault Error

Can anybodyy help me with this. I am trying run an application using the cmakefiles. on the main file of my program I get a segmentation fault when the program gets to the line of code to execute the QAppication. Here is the fragment code below:
int main(int argc, char** argv)
{
bool viewing;
parse_command_line( argc, argv );
#ifdef _GRAPHICS_
glutInit(&argc, argv);
#endif
if( viewing )
{
#ifdef _GRAPHICS_
QApplication application(argc, argv);
Viewer *viewer = new Viewer( 0, exp, argc, argv );
Interface *render = new Interface( 0, exp, viewer );
render->show();
return application.exec(); //this line causes the segmentation fault
delete viewer;
delete render;
#endif
}
}
Your problem is that the application object is destroyed prior to other objects that use it. You should leverage the C++ semantics to help you with that - and get rid of the abhorrent manual memory management while you're at it:
int main(int argc, char** argv)
{
bool viewing;
parse_command_line( argc, argv );
#ifdef _GRAPHICS_
glutInit(&argc, argv);
#endif
if (viewing) {
#ifdef _GRAPHICS_
QApplication app(argc, argv);
Viewer viewer( 0, exp, argc, argv );
Interface render( 0, exp, &viewer );
render.show();
return app.exec();
#endif
}
}
The above is necessary - thus you must make the change to avoid undefined behavior. But the fix may not be sufficient if your Viewer or Interface implementations have bugs.

How I show application when open application again Qt

Now, I have 1 application, but I don't want to open application twice, so I using QShareMemory to detect application when open twice.
And my question is: how I show current application in screen when user open application the second ?
int main(int argc, char *argv[]) {
Application a(argc, argv);
/*Make sure only one instance of application can run on host system at a time*/
QSharedMemory sharedMemory;
sharedMemory.setKey ("Application");
if (!sharedMemory.create(1))
{
qDebug() << "123123Exit already a process running";
return 0;
}
/**/
return a.exec();
}
Thanks.
Here's another approach in pure Qt way:
Use QLocalServer and QLocalSocket to check the existence of application and then use signal-slot mechanism to notify the existing one.
#include "widget.h"
#include <QApplication>
#include <QObject>
#include <QLocalSocket>
#include <QLocalServer>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
const QString appKey = "applicationKey";
QLocalSocket *socket = new QLocalSocket();
socket->connectToServer(appKey);
if (socket->isOpen()) {
socket->close();
socket->deleteLater();
return 0;
}
socket->deleteLater();
Widget w;
QLocalServer server;
QObject::connect(&server,
&QLocalServer::newConnection,
[&w] () {
/*Set the window on the top level.*/
w.setWindowFlags(w.windowFlags() |
Qt::WindowStaysOnTopHint);
w.showNormal();
w.setWindowFlags(w.windowFlags() &
~Qt::WindowStaysOnTopHint
);
w.showNormal();
w.activateWindow();
});
server.listen(appKey);
w.show();
return a.exec();
}
But if you're using Qt 5.3 on Windows, there's a bug for QWidget::setWindowFlags and Qt::WindowStaysOnTopHint, see https://bugreports.qt.io/browse/QTBUG-30359.
Just use QSingleApplication class instead of QApplication:
https://github.com/qtproject/qt-solutions/tree/master/qtsingleapplication
int main(int argc, char **argv)
{
QtSingleApplication app(argc, argv);
if (app.isRunning())
return 0;
MyMainWidget mmw;
app.setActivationWindow(&mmw);
mmw.show();
return app.exec();
}
It is part of Qt Solutions: https://github.com/qtproject/qt-solutions

Program crashes when play video with QWebView

Here is the code
#include <QApplication>
#include <QtWebKitWidgets/QWebView>
int main(int argc, char * argv[])
{
QApplication app(argc, argv);
QWebView view;
QCoreApplication::addLibraryPath("./plugins");
view.settings()->setAttribute(QWebSettings::PluginsEnabled, true);
// crash here
view.load(QUrl("http://hon.qq.com/act/20140320video/Aluna.html"));
// OK to show youtube
//view.load(QUrl("http://www.youtube.com/watch?v=KMU0tzLwhbE"));
view.show();
return app.exec();
}
It crashes when play video on hon.qq.com, but works well playing video on Youtube.