PageRenderTime 42ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/qt-everywhere-opensource-src-4.8.2/doc/html/network-threadedfortuneserver.html

#
HTML | 149 lines | 138 code | 7 blank | 4 comment | 0 complexity | 81e124b50e37a577e1b40605f093bd09 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-4.0, LGPL-3.0, GPL-2.0, LGPL-2.0, LGPL-2.1, GPL-3.0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <!-- threadedfortuneserver.qdoc -->
  7. <title>Qt 4.8: Threaded Fortune Server Example</title>
  8. <link rel="stylesheet" type="text/css" href="style/offline.css" />
  9. </head>
  10. <body>
  11. <div class="header" id="qtdocheader">
  12. <div class="content">
  13. <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
  14. </div>
  15. <div class="breadcrumb toolblock">
  16. <ul>
  17. <li class="first"><a href="index.html">Home</a></li>
  18. <!-- Breadcrumbs go here -->
  19. <li><a href="all-examples.html">Examples</a></li>
  20. <li>Threaded Fortune Server Example</li>
  21. </ul>
  22. </div>
  23. </div>
  24. <div class="content mainContent">
  25. <h1 class="title">Threaded Fortune Server Example</h1>
  26. <span class="subtitle"></span>
  27. <!-- $$$network/threadedfortuneserver-description -->
  28. <div class="descr"> <a name="details"></a>
  29. <p>Files:</p>
  30. <ul>
  31. <li><a href="network-threadedfortuneserver-dialog-cpp.html">network/threadedfortuneserver/dialog.cpp</a></li>
  32. <li><a href="network-threadedfortuneserver-dialog-h.html">network/threadedfortuneserver/dialog.h</a></li>
  33. <li><a href="network-threadedfortuneserver-fortuneserver-cpp.html">network/threadedfortuneserver/fortuneserver.cpp</a></li>
  34. <li><a href="network-threadedfortuneserver-fortuneserver-h.html">network/threadedfortuneserver/fortuneserver.h</a></li>
  35. <li><a href="network-threadedfortuneserver-fortunethread-cpp.html">network/threadedfortuneserver/fortunethread.cpp</a></li>
  36. <li><a href="network-threadedfortuneserver-fortunethread-h.html">network/threadedfortuneserver/fortunethread.h</a></li>
  37. <li><a href="network-threadedfortuneserver-main-cpp.html">network/threadedfortuneserver/main.cpp</a></li>
  38. <li><a href="network-threadedfortuneserver-threadedfortuneserver-pro.html">network/threadedfortuneserver/threadedfortuneserver.pro</a></li>
  39. </ul>
  40. <p>The Threaded Fortune Server example shows how to create a server for a simple network service that uses threads to handle requests from different clients.<p>The example is intended to be run alongside the Fortune Client example.</p>
  41. <p class="centerAlign"><img src="images/threadedfortuneserver-example.png" alt="" /></p><p>The implementation of this example is similar to that of the <a href="network-fortuneserver.html">Fortune Server</a> example, but here we will implement a subclass of <a href="qtcpserver.html">QTcpServer</a> that starts each connection in a different thread.</p>
  42. <p>For this we need two classes: FortuneServer, a <a href="qtcpserver.html">QTcpServer</a> subclass, and FortuneThread, which inherits <a href="qthread.html">QThread</a>.</p>
  43. <pre class="cpp"> <span class="keyword">class</span> FortuneServer : <span class="keyword">public</span> <span class="type"><a href="qtcpserver.html">QTcpServer</a></span>
  44. {
  45. Q_OBJECT
  46. <span class="keyword">public</span>:
  47. FortuneServer(<span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);
  48. <span class="keyword">protected</span>:
  49. <span class="type">void</span> incomingConnection(<span class="type">int</span> socketDescriptor);
  50. <span class="keyword">private</span>:
  51. <span class="type"><a href="qstringlist.html">QStringList</a></span> fortunes;
  52. };</pre>
  53. <p>FortuneServer inherits <a href="qtcpserver.html">QTcpServer</a> and reimplements <a href="qtcpserver.html#incomingConnection">QTcpServer::incomingConnection</a>(). We also use it for storing the list of random fortunes.</p>
  54. <pre class="cpp"> FortuneServer<span class="operator">::</span>FortuneServer(<span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>parent)
  55. : <span class="type"><a href="qtcpserver.html">QTcpServer</a></span>(parent)
  56. {
  57. fortunes <span class="operator">&lt;</span><span class="operator">&lt;</span> tr(<span class="string">&quot;You've been leading a dog's life. Stay off the furniture.&quot;</span>)
  58. <span class="operator">&lt;</span><span class="operator">&lt;</span> tr(<span class="string">&quot;You've got to think about tomorrow.&quot;</span>)
  59. <span class="operator">&lt;</span><span class="operator">&lt;</span> tr(<span class="string">&quot;You will be surprised by a loud noise.&quot;</span>)
  60. <span class="operator">&lt;</span><span class="operator">&lt;</span> tr(<span class="string">&quot;You will feel hungry again in another hour.&quot;</span>)
  61. <span class="operator">&lt;</span><span class="operator">&lt;</span> tr(<span class="string">&quot;You might have mail.&quot;</span>)
  62. <span class="operator">&lt;</span><span class="operator">&lt;</span> tr(<span class="string">&quot;You cannot kill time without injuring eternity.&quot;</span>)
  63. <span class="operator">&lt;</span><span class="operator">&lt;</span> tr(<span class="string">&quot;Computers are not intelligent. They only think they are.&quot;</span>);
  64. }</pre>
  65. <p>We use FortuneServer's constructor to simply generate the list of fortunes.</p>
  66. <pre class="cpp"> <span class="type">void</span> FortuneServer<span class="operator">::</span>incomingConnection(<span class="type">int</span> socketDescriptor)
  67. {
  68. <span class="type"><a href="qstring.html">QString</a></span> fortune <span class="operator">=</span> fortunes<span class="operator">.</span>at(qrand() <span class="operator">%</span> fortunes<span class="operator">.</span>size());
  69. FortuneThread <span class="operator">*</span>thread <span class="operator">=</span> <span class="keyword">new</span> FortuneThread(socketDescriptor<span class="operator">,</span> fortune<span class="operator">,</span> <span class="keyword">this</span>);
  70. connect(thread<span class="operator">,</span> SIGNAL(finished())<span class="operator">,</span> thread<span class="operator">,</span> SLOT(deleteLater()));
  71. thread<span class="operator">-</span><span class="operator">&gt;</span>start();
  72. }</pre>
  73. <p>Our implementation of <a href="qtcpserver.html#incomingConnection">QTcpServer::incomingConnection</a>() creates a FortuneThread object, passing the incoming socket descriptor and a random fortune to FortuneThread's constructor. By connecting FortuneThread's finished() signal to <a href="qobject.html#deleteLater">QObject::deleteLater</a>(), we ensure that the thread gets deleted once it has finished. We can then call <a href="qthread.html#start">QThread::start</a>(), which starts the thread.</p>
  74. <pre class="cpp"> <span class="keyword">class</span> FortuneThread : <span class="keyword">public</span> <span class="type"><a href="qthread.html">QThread</a></span>
  75. {
  76. Q_OBJECT
  77. <span class="keyword">public</span>:
  78. FortuneThread(<span class="type">int</span> socketDescriptor<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&amp;</span>fortune<span class="operator">,</span> <span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>parent);
  79. <span class="type">void</span> run();
  80. <span class="keyword">signals</span>:
  81. <span class="type">void</span> error(<span class="type"><a href="qtcpsocket.html">QTcpSocket</a></span><span class="operator">::</span>SocketError socketError);
  82. <span class="keyword">private</span>:
  83. <span class="type">int</span> socketDescriptor;
  84. <span class="type"><a href="qstring.html">QString</a></span> text;
  85. };</pre>
  86. <p>Moving on to the FortuneThread class, this is a <a href="qthread.html">QThread</a> subclass whose job is to write the fortune to the connected socket. The class reimplements <a href="qthread.html#run">QThread::run</a>(), and it has a signal for reporting errors.</p>
  87. <pre class="cpp"> FortuneThread<span class="operator">::</span>FortuneThread(<span class="type">int</span> socketDescriptor<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&amp;</span>fortune<span class="operator">,</span> <span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>parent)
  88. : <span class="type"><a href="qthread.html">QThread</a></span>(parent)<span class="operator">,</span> socketDescriptor(socketDescriptor)<span class="operator">,</span> text(fortune)
  89. {
  90. }</pre>
  91. <p>FortuneThread's constructor simply stores the socket descriptor and fortune text, so that they are available for run() later on.</p>
  92. <pre class="cpp"> <span class="type">void</span> FortuneThread<span class="operator">::</span>run()
  93. {
  94. <span class="type"><a href="qtcpsocket.html">QTcpSocket</a></span> tcpSocket;</pre>
  95. <p>The first thing our run() function does is to create a <a href="qtcpsocket.html">QTcpSocket</a> object on the stack. What's worth noticing is that we are creating this object inside the thread, which automatically associates the socket to the thread's event loop. This ensures that Qt will not try to deliver events to our socket from the main thread while we are accessing it from FortuneThread::run().</p>
  96. <pre class="cpp"> <span class="keyword">if</span> (<span class="operator">!</span>tcpSocket<span class="operator">.</span>setSocketDescriptor(socketDescriptor)) {
  97. <span class="keyword">emit</span> error(tcpSocket<span class="operator">.</span>error());
  98. <span class="keyword">return</span>;
  99. }</pre>
  100. <p>The socket is initialized by calling <a href="qabstractsocket.html#setSocketDescriptor">QTcpSocket::setSocketDescriptor</a>(), passing our socket descriptor as an argument. We expect this to succeed, but just to be sure, (although unlikely, the system may run out of resources,) we catch the return value and report any error.</p>
  101. <pre class="cpp"> <span class="type"><a href="qbytearray.html">QByteArray</a></span> block;
  102. <span class="type"><a href="qdatastream.html">QDataStream</a></span> out(<span class="operator">&amp;</span>block<span class="operator">,</span> <span class="type"><a href="qiodevice.html">QIODevice</a></span><span class="operator">::</span>WriteOnly);
  103. out<span class="operator">.</span>setVersion(<span class="type"><a href="qdatastream.html">QDataStream</a></span><span class="operator">::</span>Qt_4_0);
  104. out <span class="operator">&lt;</span><span class="operator">&lt;</span> (<span class="type"><a href="qtglobal.html#quint16-typedef">quint16</a></span>)<span class="number">0</span>;
  105. out <span class="operator">&lt;</span><span class="operator">&lt;</span> text;
  106. out<span class="operator">.</span>device()<span class="operator">-</span><span class="operator">&gt;</span>seek(<span class="number">0</span>);
  107. out <span class="operator">&lt;</span><span class="operator">&lt;</span> (<span class="type"><a href="qtglobal.html#quint16-typedef">quint16</a></span>)(block<span class="operator">.</span>size() <span class="operator">-</span> <span class="keyword">sizeof</span>(<span class="type"><a href="qtglobal.html#quint16-typedef">quint16</a></span>));</pre>
  108. <p>As with the <a href="network-fortuneserver.html">Fortune Server</a> example, we encode the fortune into a <a href="qbytearray.html">QByteArray</a> using <a href="qdatastream.html">QDataStream</a>.</p>
  109. <pre class="cpp"> tcpSocket<span class="operator">.</span>write(block);
  110. tcpSocket<span class="operator">.</span>disconnectFromHost();
  111. tcpSocket<span class="operator">.</span>waitForDisconnected();
  112. }</pre>
  113. <p>But unlike the previous example, we finish off by calling <a href="qabstractsocket.html#waitForDisconnected">QTcpSocket::waitForDisconnected</a>(), which blocks the calling thread until the socket has disconnected. Because we are running in a separate thread, the GUI will remain responsive.</p>
  114. </div>
  115. <p><b>See also </b><a href="network-fortuneserver.html">Fortune Server Example</a>, <a href="network-fortuneclient.html">Fortune Client Example</a>, and <a href="network-blockingfortuneclient.html">Blocking Fortune Client Example</a>.</p>
  116. <!-- @@@network/threadedfortuneserver -->
  117. <div class="ft">
  118. <span></span>
  119. </div>
  120. </div>
  121. <div class="footer">
  122. <p>
  123. <acronym title="Copyright">&copy;</acronym> 2012 Nokia Corporation and/or its
  124. subsidiaries. Documentation contributions included herein are the copyrights of
  125. their respective owners.</p>
  126. <br />
  127. <p>
  128. The documentation provided herein is licensed under the terms of the
  129. <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation
  130. License version 1.3</a> as published by the Free Software Foundation.</p>
  131. <p>
  132. Documentation sources may be obtained from <a href="http://www.qt-project.org">
  133. www.qt-project.org</a>.</p>
  134. <br />
  135. <p>
  136. Nokia, Qt and their respective logos are trademarks of Nokia Corporation
  137. in Finland and/or other countries worldwide. All other trademarks are property
  138. of their respective owners. <a title="Privacy Policy"
  139. href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
  140. </div>
  141. </body>
  142. </html>