Results 1 to 10 of 10

Thread: Simple Networking

  1. #1
    Join Date
    Oct 2008
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Simple Networking

    Hi, I'm trying to write a very simple MSN client using a few articles I've found which describe the MSN protocol. So far it doesn't work in the slightest, but I don't know if I'm doing the networking properly or not, so it's hard to troubleshoot.

    Could someone tell me the simplest way to just connect to the host, send a string ("VER 0 MSNP9 MSNP8 MSNP7"), and then wait for and output a response from the server?

  2. #2
    Join Date
    Oct 2008
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Simple Networking

    Here's a sample of what I've tried, but this doesn't work it just loops forever.
    Qt Code:
    1. // Just for a simple output window
    2. QTextBrowser *text = new QTextBrowser();
    3. text->show();
    4.  
    5. QTcpSocket *client = new QTcpSocket();
    6. client->connectToHost( tr("messenger.hotmail.com"), 1863 );
    7. client->write( "VER 0 MSNP8 MSNP7 MSNP6 MSNP5", strlen("VER 0 MSNP8 MSNP7 MSNP6 MSNP5") );
    8. while( client->bytesToWrite() > 0 )
    9. {
    10. qApp->processEvents();
    11. text->append( tr("Bytes Left: %1").arg(client->bytesToWrite()) );
    12. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Simple Networking

    I think you need to wait for a connection to be established before starting to send data.
    (either by attaching to signals or by using QAbstractSocket::waitForConnected())


    Qt Code:
    1. QTcpSocket *client = new QTcpSocket();
    2. client->connectToHost( tr("messenger.hotmail.com"), 1863 );
    3. if (!client->waitForConnected()) // not good if in GUI thread, use signals then
    4. {
    5. qDebug() << "connection failed";
    6. return false;
    7. }
    8. client->write( "VER 0 MSNP8 MSNP7 MSNP6 MSNP5", strlen("VER 0 MSNP8 MSNP7 MSNP6 MSNP5") );
    To copy to clipboard, switch view to plain text mode 
    HTH

    PS: I doubt it is necessary to tr() wrap the server url.
    PPS: Note you can use a QTextStream for writing to that socket:
    Qt Code:
    1. QTextStream out(client);
    2. out << "VER 0 MSNP8 MSNP7 MSNP6 MSNP5"
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Oct 2008
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Simple Networking

    Thank you. That works now, but I still do not understand how to read from the socket.

  5. #5
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Simple Networking

    Basically the same:
    Wait for the connection to be established and data has arrived with QIODevice::waitForReadRead(), (or connect to QIODevice::readyRead()).

    Then read. You may use a QTextStream, or e.g. readLine().
    (You have to take care that data might arrive in packages: you might have to append several packages to get a line you sent.)

  6. #6
    Join Date
    Oct 2008
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Simple Networking

    Ok, waitForReadyRead() returns false every time. Does that mean the server is simply not sending a reply?

  7. #7
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Simple Networking

    If you are connected and have waited long enough: yes.

    You can check with a tool like ethereal.
    For simple text based protocols you can also just telnet the server and paste your "command" there.

  8. #8
    Join Date
    Oct 2008
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Simple Networking

    Quote Originally Posted by caduel View Post
    For simple text based protocols you can also just telnet the server and paste your "command" there.
    How do I do that?

  9. #9
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Simple Networking

    (linux)
    telnet server port

    e.g.

    Qt Code:
    1. telnet messenger.hotmail.com 1863
    To copy to clipboard, switch view to plain text mode 
    then type what you would send (or just paste it)
    The server will reply...

    (telnet is available for windows, too. you have to check if it has to be called differently.)

    HTH

  10. #10
    Join Date
    Oct 2008
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Simple Networking

    Quote Originally Posted by caduel View Post
    (linux)
    telnet server port

    e.g.

    Qt Code:
    1. telnet messenger.hotmail.com 1863
    To copy to clipboard, switch view to plain text mode 
    then type what you would send (or just paste it)
    The server will reply...

    (telnet is available for windows, too. you have to check if it has to be called differently.)

    HTH
    It has the same syntax, but I am unable to connect. I'm going to assume for now that the server has changed since the article was written, and I'll look into it a bit more. Thank you for all your help

Similar Threads

  1. Simple way to expand all nodes on a QTreeView?
    By cboles in forum Qt Programming
    Replies: 10
    Last Post: 12th April 2014, 16:54
  2. very simple and basic FAM/Gamin wrapper for qt
    By momesana in forum Qt-based Software
    Replies: 0
    Last Post: 8th April 2008, 12:46
  3. QTextEdit simple question
    By Marcopolo in forum Qt Tools
    Replies: 4
    Last Post: 11th October 2007, 00:01
  4. Simple custom widget won't size properly
    By MrGarbage in forum Qt Tools
    Replies: 2
    Last Post: 9th August 2007, 13:12
  5. Creating simple text editor from the eamples code
    By overcast in forum Qt Programming
    Replies: 4
    Last Post: 14th February 2007, 15:46

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.