Results 1 to 11 of 11

Thread: problem with QTcpSocket

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2010
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default problem with QTcpSocket

    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:
    Qt Code:
    1. #ifndef SERVER_H
    2. #define SERVER_H
    3.  
    4. namespace Irc
    5. {
    6. class Client;
    7. class Server;
    8. } //namespace Irc
    9.  
    10. #include <QTcpServer>
    11. #include <QTcpSocket>
    12. #include <boost/shared_ptr.hpp>
    13. #include <QThread>
    14.  
    15. typedef boost::shared_ptr<QTcpSocket> SocketPtr;
    16.  
    17. class Irc::Server : public QObject
    18. {
    19. Q_OBJECT
    20. public:
    21. Server(QObject* parent = 0);
    22. ~Server();
    23.  
    24. void start();
    25.  
    26. public slots:
    27. void clientHandler();
    28.  
    29. signals:
    30. void clientConnected(SocketPtr socket);
    31.  
    32. private:
    33. QTcpServer* m_server;
    34. QThread* m_thread;
    35. };
    36. #endif
    To copy to clipboard, switch view to plain text mode 
    server.cpp:
    Qt Code:
    1. #include "server.h"
    2. #include "client.h"
    3.  
    4. #include <boost/shared_ptr.hpp>
    5. #include <iostream>
    6. #include <cstdlib>
    7.  
    8. #include "defines.h"
    9. #include "auxiliar.h"
    10. #include "configreader.h"
    11.  
    12. extern Irc::Config *g_config;
    13.  
    14. Irc::Server::Server(QObject* parent)
    15. : QObject(parent)
    16. {
    17. m_server = new QTcpServer(this);
    18. m_thread = new QThread(this);
    19. std::string ports = g_config->getString(Config::C_PORTS);
    20. StringVec p = splitString(ports, ";");
    21. for (int i = 0; i < (int)p.size(); i++) {
    22. if (!m_server->isListening() && !m_server->listen(QHostAddress::Any, atoi(p[i].c_str()))) {
    23. tcout() << "Unable to listen Error: " << m_server->errorString().toStdString() << "." << std::endl;
    24. return;
    25. }
    26. }
    27. tcout() << "Listen: Ok" << std::endl;
    28. m_thread->start();
    29. }
    30.  
    31. Irc::Server::~Server()
    32. {
    33. delete m_server;
    34. delete m_thread;
    35. }
    36.  
    37. void Irc::Server::start()
    38. {
    39. connect(m_server, SIGNAL(newConnection()), this, SLOT(clientHandler()));
    40. }
    41.  
    42. void Irc::Server::clientHandler()
    43. {
    44. SocketPtr newSocket(m_server->nextPendingConnection());
    45. emit clientConnected(newSocket);
    46. }
    To copy to clipboard, switch view to plain text mode 
    the slot thats the server emitting
    Qt Code:
    1. void Irc::MainFrame::handleConnections(SocketPtr p)
    2. {
    3. tcout() << "New connection: " << p << std::endl;
    4. (void) new Irc::Client(p);
    5. }
    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
    Last edited by Fallen_; 27th November 2010 at 11:52.

Similar Threads

  1. QTcpSocket problem
    By MSUdom5 in forum Qt Programming
    Replies: 3
    Last Post: 23rd February 2010, 15:23
  2. Problem in QTcpSocket
    By navi1084 in forum Qt Programming
    Replies: 2
    Last Post: 16th October 2008, 12:12
  3. QTcpSocket timeout problem
    By altVis in forum Qt Programming
    Replies: 1
    Last Post: 25th April 2008, 13:56
  4. problem with QTcpSocket
    By SuperSonik in forum Qt Programming
    Replies: 8
    Last Post: 31st January 2007, 16:00
  5. QThread and QTcpSocket problem
    By probine in forum Qt Programming
    Replies: 1
    Last Post: 5th April 2006, 09:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.