Results 1 to 5 of 5

Thread: simple socket client example

  1. #1
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default simple socket client example

    Hello!

    I've wrote server and client using sockets.h in C. Now I'm trying to write client in QT4. I can't find any working example. My server program sends always 20 bytes if we first send him one byte which is 'o', and I just want to read It. Anyone can help? Maybe some sample code?

    thanks in advance
    best regards
    Tomasz
    Last edited by Tomasz; 26th July 2010 at 10:25.

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: simple socket client example

    Did you go through the Qt demos ? You can find examples for Networking there.

  3. #3
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: simple socket client example

    Yes. I've found some examples at http://doc.qt.nokia.com/4.0/qt4-network.html#tcp-client. But It isn't what I'm looking for so I've read QTcpSocket class reference and tried to write something on my own:

    Qt Code:
    1. char bufor[20];
    2. tcpSocket = new QTcpSocket(this);
    3.  
    4. tcpSocket->abort();
    5. tcpSocket->connectToHost("localhost",57717);
    6.  
    7. bufor[0] = 'o';
    8. tcpSocket->write(bufor,1);
    9. tcpSocket->read(bufor,19);
    To copy to clipboard, switch view to plain text mode 

    but when I read I've got what I've just wrote ('o'). Tutorial in link above isn't clear for me. Can someone tell me what should I change in my code to make it work?

    thanks in advance
    best regards
    Tomasz

  4. The following user says thank you to Tomasz for this useful post:

    TheIndependentAquarius (20th June 2014)

  5. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: simple socket client example

    Ok: Use signals and slots. Wait until the connection is established before writing. Wait until there is something to read before reading.

  6. The following user says thank you to squidge for this useful post:

    TheIndependentAquarius (20th June 2014)

  7. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: simple socket client example


  8. The following user says thank you to aamer4yu for this useful post:

    TheIndependentAquarius (20th June 2014)

Similar Threads

  1. Client Socket
    By electronicboy in forum General Programming
    Replies: 1
    Last Post: 12th October 2009, 12:20
  2. Replies: 1
    Last Post: 27th March 2009, 13:20
  3. Help with a simple client programe
    By QuTe in forum Newbie
    Replies: 11
    Last Post: 31st October 2007, 21:41
  4. Programming client-server with socket in multi-languages
    By philiptine in forum Qt Programming
    Replies: 3
    Last Post: 7th September 2007, 07:35
  5. bad UDP socket :(
    By chaosgeorge in forum Qt Programming
    Replies: 1
    Last Post: 11th December 2006, 02:55

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
  •  
Qt is a trademark of The Qt Company.