Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > b293cf61b174f6cb974e48c9096dc9b3 > files > 653

lib64commoncpp-devel-1.8.1-7.mga4.x86_64.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.5"/>
<title>GNU CommonC++: tcpthread.cpp</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">GNU CommonC++
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.5 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">tcpthread.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><div class="line"><span class="comment">// Copyright (C) 1999-2005 Open Source Telecom Corporation.</span></div>
<div class="line"><span class="comment">// Copyright (C) 2006-2008 David Sugar, Tycho Softworks.</span></div>
<div class="line"><span class="comment">//</span></div>
<div class="line"><span class="comment">// This program is free software; you can redistribute it and/or modify</span></div>
<div class="line"><span class="comment">// it under the terms of the GNU General Public License as published by</span></div>
<div class="line"><span class="comment">// the Free Software Foundation; either version 2 of the License, or</span></div>
<div class="line"><span class="comment">// (at your option) any later version.</span></div>
<div class="line"><span class="comment">//</span></div>
<div class="line"><span class="comment">// This program is distributed in the hope that it will be useful,</span></div>
<div class="line"><span class="comment">// but WITHOUT ANY WARRANTY; without even the implied warranty of</span></div>
<div class="line"><span class="comment">// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span></div>
<div class="line"><span class="comment">// GNU General Public License for more details.</span></div>
<div class="line"><span class="comment">//</span></div>
<div class="line"><span class="comment">// You should have received a copy of the GNU General Public License</span></div>
<div class="line"><span class="comment">// along with this program; if not, write to the Free Software</span></div>
<div class="line"><span class="comment">// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.</span></div>
<div class="line"><span class="comment">//</span></div>
<div class="line"><span class="comment">// As a special exception to the GNU General Public License, permission is</span></div>
<div class="line"><span class="comment">// granted for additional uses of the text contained in its release</span></div>
<div class="line"><span class="comment">// of APE.</span></div>
<div class="line"><span class="comment">//</span></div>
<div class="line"><span class="comment">// The exception is that, if you link the APE library with other files</span></div>
<div class="line"><span class="comment">// to produce an executable, this does not by itself cause the</span></div>
<div class="line"><span class="comment">// resulting executable to be covered by the GNU General Public License.</span></div>
<div class="line"><span class="comment">// Your use of that executable is in no way restricted on account of</span></div>
<div class="line"><span class="comment">// linking the APE library code into it.</span></div>
<div class="line"><span class="comment">//</span></div>
<div class="line"><span class="comment">// This exception does not however invalidate any other reasons why</span></div>
<div class="line"><span class="comment">// the executable file might be covered by the GNU General Public License.</span></div>
<div class="line"><span class="comment">//</span></div>
<div class="line"><span class="comment">// This exception applies only to the code released under the</span></div>
<div class="line"><span class="comment">// name APE.  If you copy code from other releases into a copy of</span></div>
<div class="line"><span class="comment">// APE, as the General Public License permits, the exception does</span></div>
<div class="line"><span class="comment">// not apply to the code that you add in this way.  To avoid misleading</span></div>
<div class="line"><span class="comment">// anyone as to the status of such modified files, you must delete</span></div>
<div class="line"><span class="comment">// this exception notice from them.</span></div>
<div class="line"><span class="comment">//</span></div>
<div class="line"><span class="comment">// If you write modifications of your own for APE, it is your choice</span></div>
<div class="line"><span class="comment">// whether to permit this exception to apply to your modifications.</span></div>
<div class="line"><span class="comment">// If you do not wish that, delete this exception notice.</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#include &lt;<a class="code" href="socket_8h.html">cc++/socket.h</a>&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;cstdlib&gt;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#ifdef  CCXX_NAMESPACES</span></div>
<div class="line"><span class="preprocessor"></span><span class="keyword">using namespace </span>std;</div>
<div class="line"><span class="keyword">using namespace </span>ost;</div>
<div class="line"><span class="preprocessor">#endif</span></div>
<div class="line"><span class="preprocessor"></span></div>
<div class="line"><span class="keyword">class </span>myTCPSocket : <span class="keyword">public</span> <a name="_a0"></a><a class="code" href="classost_1_1_t_c_p_socket.html">TCPSocket</a></div>
<div class="line">{</div>
<div class="line"><span class="keyword">protected</span>:</div>
<div class="line">        <span class="keywordtype">bool</span> onAccept(<span class="keyword">const</span> <a name="a1"></a><a class="code" href="address_8h.html#a5eba36aa908d5a479c8ba7d0bc4512b1">InetHostAddress</a> &amp;ia, <a class="code" href="namespaceost.html#a3c74a1a40c359fd349f3e3e1b96ebfc7">tpport_t</a> port);</div>
<div class="line"></div>
<div class="line"><span class="keyword">public</span>:</div>
<div class="line">        myTCPSocket(<a name="a2"></a><a class="code" href="address_8h.html#a029ebb5b43bb13b02c02f4de0a8f3c08">InetAddress</a> &amp;ia);</div>
<div class="line">};</div>
<div class="line"></div>
<div class="line"><span class="keyword">class </span>myTCPSession : <span class="keyword">public</span> <a name="_a3"></a><a class="code" href="classost_1_1_t_c_p_session.html">TCPSession</a></div>
<div class="line">{</div>
<div class="line"><span class="keyword">private</span>:</div>
<div class="line">        <span class="keyword">static</span> <a name="_a4"></a><a class="code" href="classost_1_1_mutex.html">Mutex</a> mutex;</div>
<div class="line">        <span class="keywordtype">void</span> run(<span class="keywordtype">void</span>);</div>
<div class="line">        <span class="keywordtype">void</span> <span class="keyword">final</span>(void);</div>
<div class="line"></div>
<div class="line"><span class="keyword">public</span>:</div>
<div class="line">        myTCPSession(<a class="code" href="classost_1_1_t_c_p_socket.html">TCPSocket</a> &amp;server);</div>
<div class="line">        <span class="keyword">static</span> <span class="keyword">volatile</span> <span class="keywordtype">int</span> count;</div>
<div class="line">};</div>
<div class="line"></div>
<div class="line">myTCPSocket::myTCPSocket(<a class="code" href="address_8h.html#a029ebb5b43bb13b02c02f4de0a8f3c08">InetAddress</a> &amp;ia) : <a class="code" href="classost_1_1_t_c_p_socket.html">TCPSocket</a>(ia, 4096) {}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">bool</span> myTCPSocket::onAccept(<span class="keyword">const</span> <a class="code" href="address_8h.html#a5eba36aa908d5a479c8ba7d0bc4512b1">InetHostAddress</a> &amp;ia, <a class="code" href="namespaceost.html#a3c74a1a40c359fd349f3e3e1b96ebfc7">tpport_t</a> port)</div>
<div class="line">{</div>
<div class="line">        cout &lt;&lt; <span class="stringliteral">&quot;accepting from: &quot;</span> &lt;&lt; ia.getHostname() &lt;&lt; <span class="stringliteral">&quot;:&quot;</span> &lt;&lt; port &lt;&lt; endl;;</div>
<div class="line">        <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keyword">volatile</span> <span class="keywordtype">int</span> myTCPSession::count = 0;</div>
<div class="line"></div>
<div class="line"><a class="code" href="classost_1_1_mutex.html">Mutex</a> myTCPSession::mutex;</div>
<div class="line"></div>
<div class="line">myTCPSession::myTCPSession(<a class="code" href="classost_1_1_t_c_p_socket.html">TCPSocket</a> &amp;server) :</div>
<div class="line"><a class="code" href="classost_1_1_t_c_p_session.html">TCPSession</a>(server)</div>
<div class="line">{</div>
<div class="line">        cout &lt;&lt; <span class="stringliteral">&quot;creating session client object&quot;</span> &lt;&lt; endl;</div>
<div class="line">        mutex.enterMutex();</div>
<div class="line">        ++count;</div>
<div class="line">        mutex.leaveMutex();</div>
<div class="line">        <span class="comment">// unsetf(ios::binary);</span></div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> myTCPSession::run(<span class="keywordtype">void</span>)</div>
<div class="line">{</div>
<div class="line">        <a class="code" href="namespaceost.html#a3c74a1a40c359fd349f3e3e1b96ebfc7">tpport_t</a> port;</div>
<div class="line">        <a name="_a5"></a><a class="code" href="classost_1_1_i_p_v4_address.html">IPV4Address</a> addr = getLocal(&amp;port);</div>
<div class="line">        *tcp() &lt;&lt; <span class="stringliteral">&quot;welcome to &quot;</span> &lt;&lt; addr.<a name="a6"></a><a class="code" href="classost_1_1_i_p_v4_address.html#aaf68688cd2253cdcd313a12ab6177167">getHostname</a>() &lt;&lt; <span class="stringliteral">&quot; from socket &quot;</span> &lt;&lt; (int)so &lt;&lt; endl;</div>
<div class="line">        mutex.enterMutex();</div>
<div class="line">        *tcp() &lt;&lt; <span class="stringliteral">&quot;called from thread &quot;</span> &lt;&lt; count &lt;&lt; endl;</div>
<div class="line">        mutex.leaveMutex();</div>
<div class="line">        sleep(5000);</div>
<div class="line">        *tcp() &lt;&lt; <span class="stringliteral">&quot;ending session&quot;</span> &lt;&lt; endl;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> myTCPSession::final(<span class="keywordtype">void</span>)</div>
<div class="line">{</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">int</span> main()</div>
<div class="line">{</div>
<div class="line">        myTCPSession *tcp;</div>
<div class="line">        <a name="a7"></a><a class="code" href="address_8h.html#a9dc0c24e79c7b6f5768fe61548395273">BroadcastAddress</a> addr;</div>
<div class="line">        addr = <span class="stringliteral">&quot;255.255.255.255&quot;</span>;</div>
<div class="line">        cout &lt;&lt; <span class="stringliteral">&quot;testing addr: &quot;</span> &lt;&lt; addr.<a class="code" href="classost_1_1_i_p_v4_address.html#aaf68688cd2253cdcd313a12ab6177167">getHostname</a>() &lt;&lt; <span class="stringliteral">&quot;:&quot;</span> &lt;&lt; 4096 &lt;&lt; endl;</div>
<div class="line">        addr = <span class="stringliteral">&quot;127.0.0.1&quot;</span>;</div>
<div class="line">        cout &lt;&lt; <span class="stringliteral">&quot;binding for: &quot;</span> &lt;&lt; addr.getHostname() &lt;&lt; <span class="stringliteral">&quot;:&quot;</span> &lt;&lt; 4096 &lt;&lt; endl;</div>
<div class="line"></div>
<div class="line">        <span class="keywordflow">try</span> {</div>
<div class="line">                myTCPSocket server(addr);</div>
<div class="line"></div>
<div class="line">                <span class="keywordflow">while</span>(server.<a name="a8"></a><a class="code" href="classost_1_1_t_c_p_socket.html#a112e8235b317f15e15f6ea264fe81eb7">isPendingConnection</a>(30000)) {</div>
<div class="line">                        cout &lt;&lt; <span class="stringliteral">&quot;before create&quot;</span> &lt;&lt; endl;</div>
<div class="line">                        tcp = <span class="keyword">new</span> myTCPSession(server);</div>
<div class="line">                        cout &lt;&lt; <span class="stringliteral">&quot;after create&quot;</span> &lt;&lt; endl;</div>
<div class="line">                        tcp-&gt;detach();</div>
<div class="line">                }</div>
<div class="line">        }</div>
<div class="line">        <span class="keywordflow">catch</span>(<a name="_a9"></a><a class="code" href="classost_1_1_socket.html">Socket</a> *socket) {</div>
<div class="line">                <a class="code" href="namespaceost.html#a3c74a1a40c359fd349f3e3e1b96ebfc7">tpport_t</a> port;</div>
<div class="line">                <span class="keywordtype">int</span> err = socket-&gt;<a name="a10"></a><a class="code" href="classost_1_1_socket.html#abfdaefd24888f2d51089bdb31894286e">getErrorNumber</a>();</div>
<div class="line">                <a class="code" href="address_8h.html#a029ebb5b43bb13b02c02f4de0a8f3c08">InetAddress</a> saddr = (<a class="code" href="address_8h.html#a029ebb5b43bb13b02c02f4de0a8f3c08">InetAddress</a>)socket-&gt;<a name="a11"></a><a class="code" href="classost_1_1_socket.html#a6fd1dffdfe9606e56fba838559ff6d67">getPeer</a>(&amp;port);</div>
<div class="line">                cerr &lt;&lt; <span class="stringliteral">&quot;socket error &quot;</span> &lt;&lt; saddr.<a class="code" href="classost_1_1_i_p_v4_address.html#aaf68688cd2253cdcd313a12ab6177167">getHostname</a>() &lt;&lt; <span class="stringliteral">&quot;:&quot;</span> &lt;&lt; port &lt;&lt; <span class="stringliteral">&quot; = &quot;</span> &lt;&lt; err &lt;&lt; endl;</div>
<div class="line">                <span class="keywordflow">if</span>(err == Socket::errBindingFailed) {</div>
<div class="line">                        cerr &lt;&lt; <span class="stringliteral">&quot;bind failed; port busy&quot;</span> &lt;&lt; endl;</div>
<div class="line">                        ::exit(-1);</div>
<div class="line">                }</div>
<div class="line">                <span class="keywordflow">else</span></div>
<div class="line">                        cerr &lt;&lt; <span class="stringliteral">&quot;client socket failed&quot;</span> &lt;&lt; endl;</div>
<div class="line">        }</div>
<div class="line">        cout &lt;&lt; <span class="stringliteral">&quot;timeout after 30 seconds inactivity, exiting&quot;</span> &lt;&lt; endl;</div>
<div class="line">        <span class="keywordflow">return</span> 0;</div>
<div class="line">}</div>
<div class="line"></div>
</div><!-- fragment --> </div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Thu Oct 17 2013 23:24:56 for GNU CommonC++ by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.5
</small></address>
</body>
</html>