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;
{
Q_OBJECT
public:
~Server();
void start();
public slots:
void clientHandler();
signals:
void clientConnected(SocketPtr socket);
private:
};
#endif
#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
To copy to clipboard, switch view to plain text mode
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
){
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);
}
#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);
}
To copy to clipboard, switch view to plain text mode
the slot thats the server emitting
void Irc::MainFrame::handleConnections(SocketPtr p)
{
tcout() << "New connection: " << p << std::endl;
(void) new Irc::Client(p);
}
void Irc::MainFrame::handleConnections(SocketPtr p)
{
tcout() << "New connection: " << p << std::endl;
(void) new Irc::Client(p);
}
To copy to clipboard, switch view to plain text mode
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
Bookmarks