PDA

View Full Version : problem with QTcpSocket



Fallen_
27th November 2010, 11:46
Hi, i been working on it all day and i can't figure it out (it was working before), the problem is that when someone connects it disconnects him immediately:
server.h:

#ifndef SERVER_H
#define SERVER_H

namespace Irc
{
class Client;
class Server;
} //namespace Irc

#include <QTcpServer>
#include <QTcpSocket>
#include <boost/shared_ptr.hpp>
#include <QThread>

typedef boost::shared_ptr<QTcpSocket> SocketPtr;

class Irc::Server : public QObject
{
Q_OBJECT
public:
Server(QObject* parent = 0);
~Server();

void start();

public slots:
void clientHandler();

signals:
void clientConnected(SocketPtr socket);

private:
QTcpServer* m_server;
QThread* m_thread;
};
#endif

server.cpp:

#include "server.h"
#include "client.h"

#include <boost/shared_ptr.hpp>
#include <iostream>
#include <cstdlib>

#include "defines.h"
#include "auxiliar.h"
#include "configreader.h"

extern Irc::Config *g_config;

Irc::Server::Server(QObject* parent)
: QObject(parent)
{
m_server = new QTcpServer(this);
m_thread = new QThread(this);
std::string ports = g_config->getString(Config::C_PORTS);
StringVec p = splitString(ports, ";");
for (int i = 0; i < (int)p.size(); i++) {
if (!m_server->isListening() && !m_server->listen(QHostAddress::Any, atoi(p[i].c_str()))) {
tcout() << "Unable to listen Error: " << m_server->errorString().toStdString() << "." << std::endl;
return;
}
}
tcout() << "Listen: Ok" << std::endl;
m_thread->start();
}

Irc::Server::~Server()
{
delete m_server;
delete m_thread;
}

void Irc::Server::start()
{
connect(m_server, SIGNAL(newConnection()), this, SLOT(clientHandler()));
}

void Irc::Server::clientHandler()
{
SocketPtr newSocket(m_server->nextPendingConnection());
emit clientConnected(newSocket);
}

the slot thats the server emitting

void Irc::MainFrame::handleConnections(SocketPtr p)
{
tcout() << "New connection: " << p << std::endl;
(void) new Irc::Client(p);
}
client.h & client.cpp: http://pastebin.com/fc2zKjHA (The text that you have entered is too long (12516 characters). Please shorten it to 10000 characters long.)
thanks
PS: it's basically an irc server

wysota
27th November 2010, 12:43
In Irc::Server::clientHandler() you are creating a socket that runs out of scope immediately due to the use of boost's shared pointer.

Fallen_
27th November 2010, 13:07
yeah, it was stored in its pointer in the class but now i removed it for some reason, is there anything to do else than storing it?
@Edit: still same thing after storing it again.

wysota
27th November 2010, 15:29
is there anything to do else than storing it?
Don't ask me, it's your program.

Fallen_
27th November 2010, 18:09
thanks for ur help

wysota
27th November 2010, 18:39
How are you "storing" the socket, by the way? Oh, and another thing. I doubt you can listen with one socket on multiple ports.

Fallen_
27th November 2010, 20:18
the listen on multi ports doesn't work well thats not the problem. I tried storing it in a vector/map or just Client* m_client; in a private slot in the class not a Qt slot, what else can i do?

wysota
27th November 2010, 21:59
Are you still using boost's shared pointer for the tcp socket?

Fallen_
27th November 2010, 22:51
yeah, and it was working before with it

wysota
28th November 2010, 00:20
Well the code is that uses it is not correct. Of course you can continue this path and wonder why your connections break or get rid of the smart pointer which is completely useless here. You get ownership of the socket so nobody is going to delete it behind your back.

Fallen_
28th November 2010, 11:03
this has been fixed. boost shared pointers ftw