Results 1 to 11 of 11

Thread: problem with QTcpSocket

  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 12:52.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with QTcpSocket

    In Irc::Server::clientHandler() you are creating a socket that runs out of scope immediately due to the use of boost's shared pointer.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2010
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem with QTcpSocket

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with QTcpSocket

    Quote Originally Posted by Fallen_ View Post
    is there anything to do else than storing it?
    Don't ask me, it's your program.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Aug 2010
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem with QTcpSocket

    thanks for ur help

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with QTcpSocket

    How are you "storing" the socket, by the way? Oh, and another thing. I doubt you can listen with one socket on multiple ports.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Aug 2010
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem with QTcpSocket

    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?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with QTcpSocket

    Are you still using boost's shared pointer for the tcp socket?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Aug 2010
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem with QTcpSocket

    yeah, and it was working before with it

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with QTcpSocket

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Aug 2010
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem with QTcpSocket

    this has been fixed. boost shared pointers ftw

Similar Threads

  1. QTcpSocket problem
    By MSUdom5 in forum Qt Programming
    Replies: 3
    Last Post: 23rd February 2010, 16:23
  2. Problem in QTcpSocket
    By navi1084 in forum Qt Programming
    Replies: 2
    Last Post: 16th October 2008, 13:12
  3. QTcpSocket timeout problem
    By altVis in forum Qt Programming
    Replies: 1
    Last Post: 25th April 2008, 14:56
  4. problem with QTcpSocket
    By SuperSonik in forum Qt Programming
    Replies: 8
    Last Post: 31st January 2007, 17:00
  5. QThread and QTcpSocket problem
    By probine in forum Qt Programming
    Replies: 1
    Last Post: 5th April 2006, 10: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.