Results 1 to 2 of 2

Thread: Write contents of std::stringstream to a socket (or connect them)

  1. #1
    Join Date
    May 2011
    Posts
    22
    Qt products
    Qt4

    Default Write contents of std::stringstream to a socket (or connect them)

    Dear all,

    I have a question on using a std::stringstream to write to a QTcpSocket, so the contents will be transfered over the network connection.

    I have a parent object:
    A QObject with a private QTcpSocket attribute called socket.

    When socket sends it readyRead() signal to parent, the parent creates a child (a QObject and QRunnable) and starts running its run();

    Due to an API implementation, the child produces an std::stringstream output, which is the output of some calculation.

    What could I do to take output (or contents of output) from child and give it to the parent, so parent can write its contents to the socket? Or maybe if connect socket->write() with output?

    Thank you in advance for any hints!

    Cheers

  2. #2
    Join Date
    May 2011
    Posts
    22
    Qt products
    Qt4

    Question Re: Write contents of std::stringstream to a socket (or connect them)

    Ok, that was easier then thought:
    Apparently dup2() (never heard of it..) can redirect stdout and stderr to another (file or socket)descriptor (sorry, never heard of that either... ).

    Whenever the QTcpServer gets an incoming connection, a incomingConnection(int handle) is emitted. This handle can be used in order to redirect all stdout ans stderr over a newly created socket.
    Qt Code:
    1. void Server::incomingConnection(int handle)
    2. {
    3. Client *client = new Client(this);
    4. client->setSocket(handle);
    5. }
    To copy to clipboard, switch view to plain text mode 
    In client:
    Qt Code:
    1. void Client::setSocket(int descriptor)
    2. {
    3. socket = new QTcpSocket(this);
    4. socket->setSocketDescriptor(descriptor);
    5.  
    6. dup2(descriptor, 1); // stdout
    7. dup2(descriptor, 2); // stderr
    8.  
    9. qDebug() << "This text ends up at the telnet-session of the connected client";
    10.  
    11. // connect socket signals to slots
    12. connect(socket, SIGNAL(connected()), this, SLOT(socketConnected()));
    13. connect(socket, SIGNAL(readyRead()), this, SLOT(socketReadyRead()));
    14.  
    15. qDebug() << "Socket created";
    16. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Wait for socket to connect without application hang?
    By hakermania in forum Qt Programming
    Replies: 5
    Last Post: 9th October 2011, 12:03
  2. why can't i write some string to the socket
    By succulent_lily in forum Qt Programming
    Replies: 5
    Last Post: 3rd December 2008, 10:26
  3. socket read/write bytes
    By nowire75 in forum Newbie
    Replies: 3
    Last Post: 5th July 2007, 00:12
  4. Replies: 4
    Last Post: 10th November 2006, 16:38
  5. How to write on a socket in another thread?
    By Valheru in forum Qt Programming
    Replies: 7
    Last Post: 12th October 2006, 11:52

Tags for this Thread

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.