QTcpServer - program finishes after listen
-
wrote on 4 Jul 2013, 11:30 last edited by
Hello all.
I'm new with Qt so please excuse me for a simple question.
I need to create a simple tcp client-server application. I found a "youtube video":http://www.youtube.com/watch?v=BSdKkZNEKlQ explaining how to use QTcpServer. Problem is that my program finishes before client doesnt manage to connect.
What is the problem? I'm not well with streams and threads, does listen() method start a new thread?
Thank you for your answersserver.pro
[code]QT += core network
QT -= guiTARGET = server
CONFIG += console
CONFIG -= app_bundleTEMPLATE = app
SOURCES += main.cpp
myserver.cppHEADERS +=
myserver.h[/code]myserver.h
[code]#ifndef MYSERVER_H
#define MYSERVER_H#include <QObject>
#include <QDebug>
#include <QTcpServer>
#include <QTcpSocket>class MyServer : public QObject
{
Q_OBJECT
public:
explicit MyServer(QObject *parent = 0);
signals:
public slots:
void acceptConnection();private:
QTcpServer *server;
};#endif // MYSERVER_H
[/code]myserver.cpp
[code]#include "myserver.h"MyServer::MyServer(QObject *parent) :
QObject(parent)
{
server = new QTcpServer(this);connect(server, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
if( !server->listen(QHostAddress::Any,1234))
printf("Server could not start\r\n");
else
printf("Server started\r\n");
}void MyServer::acceptConnection()
{
QTcpSocket *socket = server->nextPendingConnection();socket->write("hello client\r\n");
socket->flush();socket->waitForBytesWritten(3000);
socket->close();
}
[/code]main.cpp
[code]#include <QCoreApplication>
#include <stdio.h>
#include "myserver.h"int main(int argc, char * argv[])
{
QCoreApplication a(argc, argv);
MyServer mServer;
printf( "finished" );
return a.exec();
}
[/code]telnet.exe
[code]open 127.0.0.1 1234[/code] -
wrote on 4 Jul 2013, 11:45 last edited by
Problem suddenly was solved. I did nothing ^_^
-
wrote on 4 Jul 2013, 22:44 last edited by
I would just note that I have found sometimes when developing I can get some build errors, then undo my changes and still have errors. Then when I delete all my objects / output files and do a full re-build the problem goes away.
Sometimes I think the build process gets the files it needs to re-compile or not confused and this can cause strange issues...
That's just a note, its not a concrete issue, just something I have observed.
-
wrote on 5 Jul 2013, 11:19 last edited by
When in doubt it is always a good thing to remove all object files and recompile. That's standard compilation mixup fix.
Greetz
1/4