Results 1 to 15 of 15

Thread: Socket not connecting

  1. #1
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Socket not connecting

    I'm trying to connect to this server. The address is correct and the same with the port.

    Qt Code:
    1. #include "MSNSocket.h"
    2. using namespace ShadowMSN;
    3.  
    4. MSNSocket::MSNSocket(const QString& address, quint16 port, QObject* parent)
    5. : QTcpSocket(parent),
    6. m_address(address),
    7. m_port(port)
    8. {
    9. connect(this, SIGNAL(readyRead()),
    10. this, SLOT(receiveCommand()));
    11. connect(this, SIGNAL(connected()),
    12. this, SLOT(msnConnected()));
    13. connect(this, SIGNAL(error(QAbstractSocket::SocketError)),
    14. this, SLOT(errorHandling(QAbstractSocket::SocketError)));
    15. connect(this, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
    16. this, SLOT(stateWasChanged(QAbstractSocket::SocketState)));
    17. m_socket = new QTcpSocket;
    18. this->connectToHost(address, port);
    19. }
    20.  
    21. QString MSNSocket::sendCommand(const QString& command)
    22. {
    23. if(command.isEmpty()) {
    24. return " ";
    25. }
    26.  
    27. m_msnCommand.parseCommand(command.toUtf8());
    28. this->write(m_msnCommand.toString().toUtf8());
    29. return m_msnCommand.toString().toUtf8();
    30. }
    31.  
    32. void MSNSocket::receiveCommand()
    33. {
    34. //if(this->bytesAvailable())
    35. //{
    36. char* data = new char[this->bytesAvailable()];
    37. qDebug() << this->read(data, this->bytesAvailable());
    38. qDebug() << data;
    39. //}
    40. //else
    41. //{
    42. //qDebug() << "No data received";
    43. //}
    44. }
    45.  
    46. void MSNSocket::errorHandling(QAbstractSocket::SocketError error)
    47. {
    48. qDebug() << "Error: " << error;
    49. }
    50.  
    51. void MSNSocket::msnConnected()
    52. {
    53. qDebug() << "Connected";
    54. }
    55.  
    56. void MSNSocket::stateWasChanged(QAbstractSocket::SocketState state){
    57. qDebug() << "New State: " << state;
    58. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. './shadowmsn'
    2. New State: QAbstractSocket::HostLookupState
    To copy to clipboard, switch view to plain text mode 

  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: Socket not connecting

    Looks like your DNS can't find the address of the host.

  3. #3
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Socket not connecting

    I had it where it was deleting the socket object before being processed. But now.
    the socket is not working like I need for the communication to work with the server. It's sending all my commands first then it read the server's which is causing the connection to sever.

    For example. I have to send like "me them me them" and what its doing is "me me them disconnect" I've tried reading the socket document but it's to no availble unless im over looking something.

  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: Socket not connecting

    You have to keep the state of the connection and react only when you receive a full response from the other side (in slot connected to readyRead()).

  5. #5
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Socket not connecting

    I'm not sure that you mean?

  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: Socket not connecting

    You can't just write to the socket everything and then wait for answers for everything. You should only send another command when you receive answer to the previous one, and that's done in a slot connected to readyRead() signal. Refer to the docs for details.

  7. #7
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Socket not connecting

    Okay i've rewrote my code to do like I wanted but it still wants to write one command after the other. Its forcing its self >.<

    I've tried to use variable basing on it like m_canRead m_canWrite. doesn't help.

    Qt Code:
    1. #include "MSNSocket.hpp"
    2. namespace MSNP
    3. {
    4. MSNSocket::MSNSocket( QObject *parent )
    5. : QObject( parent )
    6. {
    7. m_canWrite = true;
    8. m_canRead = false;
    9. m_connected = false;
    10. m_blocksize = 0;
    11. m_buffer.clear();
    12.  
    13. m_socket = new QTcpSocket;
    14.  
    15. QObject::connect( m_socket, SIGNAL( connected() ),
    16. this, SLOT( msnConnected() ) );
    17. QObject::connect( m_socket, SIGNAL( disconnected() ),
    18. this, SLOT( msnDisconnected() ) );
    19. QObject::connect( m_socket, SIGNAL( readyRead() ),
    20. this, SLOT( receiveData() ) );
    21. QObject::connect( m_socket, SIGNAL( error( QAbstractSocket::SocketError ) ),
    22. this, SLOT( socketError( QAbstractSocket::SocketError ) ) );
    23. QObject::connect( m_socket, SIGNAL( stateChanged( QAbstractSocket::SocketState ) ),
    24. this, SLOT( stateWasChanged( QAbstractSocket::SocketState ) ) );
    25. }
    26.  
    27. MSNSocket::~MSNSocket()
    28. {
    29. }
    30.  
    31. void MSNSocket::connect( const QString& address, const quint16 port )
    32. {
    33. if ( isConnected() ) {
    34. return;
    35. }
    36.  
    37. m_blocksize = 0;
    38. m_buffer.clear();
    39.  
    40. m_socket->connectToHost( address, port );
    41. }
    42.  
    43. void MSNSocket::disconnect()
    44. {
    45. if ( !isConnected() ) {
    46. return;
    47. }
    48. m_socket->abort();
    49. }
    50.  
    51. void MSNSocket::sendCommand( QString command, MSNSocket::CommandState state )
    52. {
    53. if ( !m_socket || !m_canWrite)
    54. return;
    55.  
    56. m_msnCommand.parseCommand( command, true );
    57. MSNLog::writeSend(m_msnCommand.toString());
    58.  
    59. m_socket->write( m_msnCommand.toString().toUtf8() );
    60. m_canWrite = false;
    61. m_canRead = true;
    62. emit parseCommand( m_msnCommand );
    63. }
    64.  
    65. void MSNSocket::readBlock( quint16 size )
    66. {
    67. if ( m_blocksize ) {
    68. return;
    69. }
    70.  
    71. m_blocksize = size;
    72. checkBlock();
    73. }
    74.  
    75. bool MSNSocket::isConnected()
    76. {
    77. return m_connected;
    78. }
    79.  
    80. void MSNSocket::setConnected( bool status )
    81. {
    82. if ( m_connected == status ) {
    83. return;
    84. }
    85.  
    86. m_connected = status;
    87. }
    88.  
    89. MSNCommand& MSNSocket::getLastCommand()
    90. {
    91. return m_msnCommand;
    92. }
    93.  
    94. void MSNSocket::msnConnected()
    95. {
    96. m_connected = true;
    97. emit connected();
    98. }
    99.  
    100. void MSNSocket::msnDisconnected()
    101. {
    102. setConnected( false );
    103. emit disconnected();
    104. }
    105.  
    106. void MSNSocket::receiveData()
    107. {
    108. quint64 available = m_socket->bytesAvailable();
    109. qDebug() << available;
    110.  
    111. char *buffer = new char[available + 1];
    112. quint64 readBytes = m_socket->read( buffer, available );
    113. m_canWrite = true;
    114. m_canRead = false;
    115. m_msnCommand.parseCommand( buffer, false );
    116. MSNLog::writeReceive(m_msnCommand.toString());
    117.  
    118. if ( readBytes > 0 ) {
    119. MSNLog::writeInfo( QString( "Bytes read returned %1" ).arg( readBytes ) );
    120. } else if ( readBytes == 0 ) {
    121. MSNLog::writeWarning( QString( "Bytes returned no data" ) );
    122. } else {
    123. if ( available ) {
    124. if ( available != readBytes ) {
    125. MSNLog::writeWarning(
    126. QString( "Bytes read reported %1 bytes available but only read %2" ).arg( available ).arg( readBytes ) );
    127. }
    128. }
    129.  
    130. m_buffer.append( buffer );
    131. }
    132. // clean up.
    133. delete []buffer;
    134. }
    135.  
    136. bool MSNSocket::checkBlock()
    137. {
    138. if ( !m_blocksize ) {
    139. return false;
    140. } else if ( static_cast<quint64>( m_buffer.size() ) < m_blocksize ) {
    141. MSNLog::writeInfo( QString( "Waiting for data %1. Received %2/%3" ).arg( m_buffer.size() ).arg( m_blocksize ) );
    142. return true;
    143. }
    144.  
    145. QByteArray block = m_buffer.take( m_blocksize );
    146.  
    147. m_blocksize = 0;
    148. emit blockRead( block );
    149. return false;
    150. }
    151. } // End of MSNP
    To copy to clipboard, switch view to plain text mode 

  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: Socket not connecting

    The fact that you receive something doesn't yet mean you have received the whole command - you might have received a single byte. You should check that in the reading method.

    Could you also show us how you use the above code in practice?

  9. #9
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Socket not connecting

    Qt Code:
    1. #ifndef TEST_H
    2. #define TEST_H
    3.  
    4. #include "MSNSocket.hpp"
    5. #include "MSNCommand.hpp"
    6. using namespace MSNP;
    7.  
    8. class Test : public MSNP::MSNSocket
    9. {
    10. Q_OBJECT
    11. public:
    12. Test(const QString& email, const QString& password, QObject *parent = NULL)
    13. : MSNP::MSNSocket(parent)
    14. {
    15. m_email = email;
    16. m_password = password;
    17. QObject::connect(this, SIGNAL(connected()), this, SLOT(onConnection()));
    18. QObject::connect(this, SIGNAL(blockRead(QByteArray&)), this, SLOT(receiveCommands(QByteArray&)));
    19. QObject::connect(this, SIGNAL(parseCommand(MSNCommand&)), this, SLOT(parseCommands(MSNCommand&)));
    20. connect("messenger.hotmail.com", 1863);
    21. }
    22. public slots:
    23. void onConnection()
    24. {
    25. MSNLog::writeInfo( "Connected" );
    26. sendCommand("VER MSNP15 MSNP14 CV0");
    27. }
    28.  
    29. void parseCommands(MSNCommand& commands)
    30. {
    31. QString cmds = commands.getCommand();
    32. if(cmds.contains("VER", Qt::CaseSensitive)) {
    33. sendCommand("CVR 0x0409 unix gentoo i686 ShadowMSN 0.1 MSNSGS " + m_email);
    34. }
    35. if(cmds.contains("CVR", Qt::CaseSensitive)) {
    36. sendCommand("USR TWN I " + m_email);
    37. }
    38. if(cmds.contains("XFR", Qt::CaseSensitive)) {
    39. QStringList ip_port = commands.getParameters().split( " " );
    40. QStringList ip_port_split = ip_port[1].split(":");
    41.  
    42. connect(ip_port_split[0], ip_port_split[1].toULong());
    43. }
    44. }
    45.  
    46. void receiveCommands(QByteArray& commands)
    47. {
    48. }
    49. private:
    50. QString m_email;
    51. QString m_password;
    52. };
    53.  
    54. #endif // TEST_H
    To copy to clipboard, switch view to plain text mode 

  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: Socket not connecting

    Ok, and what exactly happens? What is being sent to the network?

  11. #11
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Socket not connecting

    eckos@Narf ~/Samples $ ./samples
    New State: QAbstractSocket::HostLookupState
    New State: QAbstractSocket::ConnectingState
    New State: QAbstractSocket::ConnectedState
    [Info] "Connected"
    [SEND] "VER 1 MSNP15 MSNP14 CV0
    "
    [SEND] "CVR 2 0x0409 unix gentoo i686 ShadowMSN 0.1 MSNSGS sample@live.com
    "
    [SEND] "USR 3 TWN I sample@live.com
    "
    149
    [RECV] "VER 3 MSNP15 MSNP14
    CVR 2 1.0.0000 1.0.0000 1.0.0000 http://msgr.dlservice.microsoft.com http://messenger.msn.com
    XFR 3 NS 207.46.106.58:1863 U D

    "
    [Info] "Bytes read returned 149"
    [ERROR] QAbstractSocket::RemoteHostClosedError
    New State: QAbstractSocket::ClosingState
    New State: QAbstractSocket::UnconnectedState
    The socket specs requires a switch style commands being sent.
    For example.
    Send
    Receive
    Send
    Receive.

  12. #12
    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: Socket not connecting

    Where is the code that triggers sending commands 2 and 3?

  13. #13
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Socket not connecting

    In Test::parseCommands() sends the commands.
    In MSNSocket sendCommand writes it to the socket.
    Last edited by jpn; 13th March 2008 at 06:54. Reason: disabled smilies

  14. #14
    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: Socket not connecting

    I see now. When you send a command, you emit a signal that triggers sending the next command and here is your problem - all the commands are send practically at once without even checking if there is anything to be read from the socket. You should do it more or less like so:

    Qt Code:
    1. enum State { Off, VerSent, CvrSent, Usr3Sent } state = Off;
    2. ...::onConnected() { sendCommand("Ver"); }
    3. ...::sendCommand(...) {
    4. switch(state){
    5. case Off: if(... == "Ver"){ socket.write(...); state = VerSent; }; break;
    6. case VerSent: if(...){ socket.write(...); state = CvrSent; }; break;
    7. ...
    8. }
    9. }
    10. ::onReadyRead(){
    11. if(!socket.canReadLine()) return;
    12. switch(state){
    13. case Off: /* shouldn't happen */ break;
    14. case VerSent: sendCommand("CVR"); break;
    15. case CvrSent: sendCommand("USR"); break;
    16. default: /* regular traffic */
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    Sending command should be performed upon receiving response to a previous command, not upon sending the command as this way you'll send all commands at once without waiting for any response.

  15. #15
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Socket not connecting

    Nevermind I finally got it working
    Last edited by ComaWhite; 11th March 2008 at 03:34.

Similar Threads

  1. Greenphone mini USB socket problem
    By izico in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 25th September 2007, 11:59
  2. Socket bind error
    By rburge in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2007, 00:18
  3. Difference between normal socket and QTcpSocket
    By ShaChris23 in forum Newbie
    Replies: 1
    Last Post: 16th June 2007, 04:38
  4. question about socket and threads?
    By oob2 in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2007, 11:42
  5. How to write on a socket in another thread?
    By Valheru in forum Qt Programming
    Replies: 7
    Last Post: 12th October 2006, 10:52

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.