Results 1 to 18 of 18

Thread: Write to Threaded Server

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Write to Threaded Server

    Why do you call disconnectFromHost if you want a reply?

  2. #2
    Join Date
    Jun 2010
    Posts
    13
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Write to Threaded Server

    ohh sure - but i fixed it already - this i shouldn't do of course. But it doesn't work anyway :-?

    tcpsocket.cpp

    Qt Code:
    1. CTcpSocket::CTcpSocket(int socketDescriptor, QObject *parent)
    2. : QObject(parent)
    3. , m_socketDescriptor(socketDescriptor)
    4. {
    5. m_tcpSocket = new QTcpSocket();
    6.  
    7. if (!m_tcpSocket->setSocketDescriptor(m_socketDescriptor)) {
    8. qDebug() << m_tcpSocket->error();
    9. return;
    10. }
    11.  
    12. connect(m_tcpSocket, SIGNAL(readyRead()), SLOT(readData()));
    13. }
    14.  
    15.  
    16. void CTcpSocket::readData()
    17. {
    18. //ToDo Implement reading Data
    19. qDebug() << "try to read";
    20. //m_tcpSocket->waitForReadyRead(10000);
    21. }
    22.  
    23.  
    24. void CTcpSocket::writeData(QString txt)
    25. {
    26. qDebug() << "try to write";
    27.  
    28. QByteArray block;
    29. QDataStream out(&block, QIODevice::WriteOnly);
    30. out.setVersion(QDataStream::Qt_4_0);
    31. out << (quint16)0;
    32. out << txt;
    33. out.device()->seek(0);
    34. out << (quint16)(block.size() - sizeof(quint16));
    35.  
    36. m_tcpSocket->write(block);
    37.  
    38. m_tcpSocket->waitForBytesWritten(10000);
    39. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Write to Threaded Server

    One reason you're not being able to read is because there's nothing to read.
    Are you sure you are receiving data? Are you sure that what you send is correct?

  4. #4
    Join Date
    Jun 2010
    Posts
    13
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Write to Threaded Server

    There is something to read for sure - I've a working not threaded server. There the message arrives.

    Client is like this:

    Qt Code:
    1. InstructionGen::InstructionGen(..)
    2. {
    3. ...
    4. socket = new QTcpSocket(this);
    5. ...
    6. socket->connectToHost(server->text(), port->value());
    7. ...
    8. }
    9.  
    10. void InstructionGen::sendInstruction()
    11. {
    12. socket->write(prefix->text().toLatin1() + message->text().toLatin1() + "\n");
    13. log_stat->append((prefix->text().toLatin1() + message->text().toLatin1() + "\n").simplified());
    14. message->clear();
    15. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Write to Threaded Server

    Where do you call void InstructionGen::sendInstruction() in your server?

    Edit: and you don't need to use threads and the socket's blocking functions.

  6. #6
    Join Date
    Jun 2010
    Posts
    13
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Write to Threaded Server

    InstructionGen is the client, which should send data to the server.

    Thx for the hint with the blocking functions... but should work anyway - am I right?

  7. #7
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Write to Threaded Server

    You have lost me completely.
    Can you post the complete code please?

  8. #8
    Join Date
    Jun 2010
    Posts
    13
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Write to Threaded Server

    Okay: server is complete.

    Now the Client: its actual the ChatSample Client in a slightly different form.

    Client:

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "instructiongen.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. InstructionGen generator;
    8. generator.show();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    instructiongen.cpp
    Qt Code:
    1. #include "instructiongen.h"
    2. #include <QVBoxLayout>
    3. #include <QHBoxLayout>
    4. #include <QGridLayout>
    5. #include <QPushButton>
    6. #include <QTextEdit>
    7. #include <QLabel>
    8. #include <QLineEdit>
    9. #include <QTcpSocket>
    10. #include <QBuffer>
    11. #include <QSpinBox>
    12. #include <QErrorMessage>
    13.  
    14. static const quint16 DEFAULT_PORT = 15001;
    15.  
    16. InstructionGen::InstructionGen(QWidget* parent, Qt::WFlags flags)
    17. : QWidget(parent, flags)
    18. {
    19. QVBoxLayout* main = new QVBoxLayout(this);
    20. QHBoxLayout* bottom = new QHBoxLayout;
    21.  
    22. QLabel* label = new QLabel("Server:", this);
    23. server = new QLineEdit(this);
    24. port = new QSpinBox(this);
    25. conn = new QPushButton("Connect", this);
    26. port->setRange(1, 32767);
    27. port->setValue(DEFAULT_PORT);
    28. server->setText("localhost");
    29. top->addWidget(label, 0, 0);
    30. top->addWidget(server, 0, 1);
    31. top->addWidget(port, 0, 2);
    32.  
    33. label = new QLabel("Prefix:", this);
    34. prefix = new QLineEdit(this);
    35. prefix->setText("sim.");
    36. top->addWidget(label, 1, 0);
    37. top->addWidget(prefix, 1, 1);
    38. top->addWidget(conn, 1, 2);
    39.  
    40. log_stat = new QTextEdit(this);
    41. log_stat->setReadOnly(true);
    42.  
    43. label = new QLabel("Instruction:", this);
    44. message = new QLineEdit(this);
    45. send = new QPushButton("Send", this);
    46. send->setDefault(true);
    47. bottom->addWidget(label);
    48. bottom->addWidget(message);
    49. bottom->addWidget(send);
    50.  
    51. main->addLayout(top);
    52. main->addWidget(log_stat);
    53. main->addLayout(bottom);
    54. setLayout(main);
    55.  
    56. buffer = new QBuffer(this);
    57. socket = new QTcpSocket(this);
    58. buffer->open(QIODevice::ReadWrite);
    59.  
    60. connect(message, SIGNAL(returnPressed()), SLOT(sendInstruction()));
    61. connect(send, SIGNAL(clicked()), SLOT(sendInstruction()));
    62. connect(conn, SIGNAL(clicked()), SLOT(toggleConnection()));
    63.  
    64. connect(socket, SIGNAL(connected()), SLOT(setConnected()));
    65. connect(socket, SIGNAL(disconnected()), SLOT(setDisconnected()));
    66. connect(socket, SIGNAL(readyRead()), SLOT(receiveMessage()));
    67.  
    68. setDisconnected();
    69. }
    70.  
    71. InstructionGen::~InstructionGen()
    72. {
    73. buffer->close();
    74. }
    75.  
    76. void InstructionGen::setConnected()
    77. {
    78. port->setEnabled(false);
    79. server->setEnabled(false);
    80. prefix->setEnabled(true);
    81. message->setEnabled(true);
    82. log_stat->setEnabled(true);
    83. log_stat->clear();
    84. send->setEnabled(true);
    85. conn->setText("Disconnect");
    86. }
    87.  
    88. void InstructionGen::setDisconnected()
    89. {
    90. port->setEnabled(true);
    91. server->setEnabled(true);
    92. prefix->setEnabled(false);
    93. message->setEnabled(false);
    94. log_stat->setEnabled(false);
    95. send->setEnabled(false);
    96. conn->setText("Connect");
    97. }
    98.  
    99. void InstructionGen::toggleConnection()
    100. {
    101. if (socket->state() == QAbstractSocket::UnconnectedState)
    102. {
    103. socket->connectToHost(server->text(), port->value());
    104. }
    105. else
    106. {
    107. socket->disconnectFromHost();
    108. }
    109. }
    110.  
    111. void InstructionGen::sendInstruction()
    112. {
    113. socket->write(prefix->text().toLatin1() + message->text().toLatin1() + "\n");
    114. log_stat->append((prefix->text().toLatin1() + message->text().toLatin1() + "\n").simplified());
    115. message->clear();
    116. }
    117.  
    118. void InstructionGen::receiveMessage()
    119. {
    120. // missing some checks for returns values for the sake of simplicity
    121. qint64 bytes = buffer->write(socket->readAll());
    122. // go back as many bytes as we just wrote so that it can be read
    123. buffer->seek(buffer->pos() - bytes);
    124. // read only full lines, line by line
    125. while (buffer->canReadLine())
    126. {
    127. QString line = buffer->readLine();
    128. log_stat->append("received: "+line.simplified());
    129. }
    130. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Write to Threaded Server

    Here's your problem
    Qt Code:
    1. connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    To copy to clipboard, switch view to plain text mode 
    Do you understand why?

    If you want to keep it simple, I've just added an example to the wiki on how to do all this without using threads.
    A lot of people seem to think they need threads for some reason.
    http://www.qtcentre.org/wiki/index.p...ithout_threads

  10. The following user says thank you to tbscope for this useful post:

    Suncell (3rd June 2010)

  11. #10
    Join Date
    Jun 2010
    Posts
    13
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Write to Threaded Server

    Outsch yeah - thx.

    And I need to loop in the run method - right?

    Why isn't a thread necessary. The program will receive tons of data out of a medical device. It should be prevent in any case blocking biosignal analysis.
    Or doesn't it matter?

  12. #11
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Write to Threaded Server

    You might want to have an event loop in your thread.

    Quote Originally Posted by Suncell View Post
    It should be prevent in any case blocking biosignal analysis
    And yet you use blocking functions :-)

    It doesn't really matter. In general, do not use threads for your connections. Do use them if you need to perform long tasks in parallel (send or receive a lot of data is not one of those tasks as it is received in packets).
    And if you don't use threads, don't use the blocking functions at all. Don't use them in threads either. The blocking functions are there for a few special cases where the client or server doesn't allow you to communicate asynchronously.

  13. #12
    Join Date
    Jun 2010
    Posts
    13
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Write to Threaded Server

    ^^ yeah I should remove them.

    But now - I'm still stuck. The readData() function is never called. Maybe deriving the Class CTcpSocket from QObject() is wrong?

    Now, server code looks like that:

    Server:

    Qt Code:
    1. MultiServer server;
    2. if (!server.listen(QHostAddress::Any, 15000)) {
    3. close();
    4. return;
    5. }
    To copy to clipboard, switch view to plain text mode 

    MultiServer.cpp
    Qt Code:
    1. MultiServer::MultiServer(QObject *parent)
    2. : QTcpServer(parent)
    3. {
    4.  
    5. }
    6.  
    7. void MultiServer::incomingConnection(int socketDescriptor)
    8. {
    9. QString msg = "Message from server";
    10. ConnectionThread *thread = new ConnectionThread(socketDescriptor, msg, this);
    11. thread->start();
    12. }
    To copy to clipboard, switch view to plain text mode 

    ConnectionThread.cpp
    Qt Code:
    1. ConnectionThread::ConnectionThread(int socketDescriptor, const QString &msg, QObject *parent)
    2. : QThread(parent)
    3. , socketDescriptor(socketDescriptor)
    4. , text(msg)
    5. {
    6.  
    7. }
    8.  
    9. void ConnectionThread::run()
    10. {
    11. my_sock = new CTcpSocket(socketDescriptor);
    12.  
    13. my_sock->writeData(text);
    14.  
    15. while(1)
    16. {
    17.  
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    tcpsocket.cpp
    Qt Code:
    1. CTcpSocket::CTcpSocket(int socketDescriptor, QObject *parent)
    2. : QObject(parent)
    3. , m_socketDescriptor(socketDescriptor)
    4. {
    5. m_tcpSocket = new QTcpSocket();
    6.  
    7. if (!m_tcpSocket->setSocketDescriptor(m_socketDescriptor)) {
    8. qDebug() << m_tcpSocket->error();
    9. return;
    10. }
    11.  
    12. connect(m_tcpSocket, SIGNAL(readyRead()), SLOT(readData()));
    13. }
    14.  
    15.  
    16. void CTcpSocket::readData()
    17. {
    18. //ToDo Implement reading Data
    19. qDebug() << "try to read";
    20. }
    21.  
    22.  
    23. void CTcpSocket::writeData(QString txt)
    24. {
    25. qDebug() << "try to write";
    26.  
    27. QByteArray block;
    28. QDataStream out(&block, QIODevice::WriteOnly);
    29. out.setVersion(QDataStream::Qt_4_0);
    30. out << (quint16)0;
    31. out << txt;
    32. out.device()->seek(0);
    33. out << (quint16)(block.size() - sizeof(quint16));
    34.  
    35. m_tcpSocket->write(block);
    36. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Simple Threaded Http Server
    By obi in forum Qt Programming
    Replies: 4
    Last Post: 20th October 2009, 10:42
  2. Network Threaded server problem
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2008, 09:08
  3. Threaded TCP server
    By Sysace in forum Newbie
    Replies: 4
    Last Post: 21st February 2008, 12:37
  4. Threaded server.
    By nithinin2001 in forum Qt Programming
    Replies: 2
    Last Post: 15th November 2007, 16:37

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