<!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"/> <title>GNU CommonC++: tcpthread.cpp</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.6.3 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main 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> <div class="contents"> <h1>tcpthread.cpp</h1><div class="fragment"><pre class="fragment"><span class="comment">// Copyright (C) 1999-2005 Open Source Telecom Corporation.</span> <span class="comment">// Copyright (C) 2006-2008 David Sugar, Tycho Softworks.</span> <span class="comment">//</span> <span class="comment">// This program is free software; you can redistribute it and/or modify</span> <span class="comment">// it under the terms of the GNU General Public License as published by</span> <span class="comment">// the Free Software Foundation; either version 2 of the License, or</span> <span class="comment">// (at your option) any later version.</span> <span class="comment">//</span> <span class="comment">// This program is distributed in the hope that it will be useful,</span> <span class="comment">// but WITHOUT ANY WARRANTY; without even the implied warranty of</span> <span class="comment">// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span> <span class="comment">// GNU General Public License for more details.</span> <span class="comment">//</span> <span class="comment">// You should have received a copy of the GNU General Public License</span> <span class="comment">// along with this program; if not, write to the Free Software</span> <span class="comment">// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.</span> <span class="comment">//</span> <span class="comment">// As a special exception to the GNU General Public License, permission is</span> <span class="comment">// granted for additional uses of the text contained in its release</span> <span class="comment">// of APE.</span> <span class="comment">//</span> <span class="comment">// The exception is that, if you link the APE library with other files</span> <span class="comment">// to produce an executable, this does not by itself cause the</span> <span class="comment">// resulting executable to be covered by the GNU General Public License.</span> <span class="comment">// Your use of that executable is in no way restricted on account of</span> <span class="comment">// linking the APE library code into it.</span> <span class="comment">//</span> <span class="comment">// This exception does not however invalidate any other reasons why</span> <span class="comment">// the executable file might be covered by the GNU General Public License.</span> <span class="comment">//</span> <span class="comment">// This exception applies only to the code released under the</span> <span class="comment">// name APE. If you copy code from other releases into a copy of</span> <span class="comment">// APE, as the General Public License permits, the exception does</span> <span class="comment">// not apply to the code that you add in this way. To avoid misleading</span> <span class="comment">// anyone as to the status of such modified files, you must delete</span> <span class="comment">// this exception notice from them.</span> <span class="comment">//</span> <span class="comment">// If you write modifications of your own for APE, it is your choice</span> <span class="comment">// whether to permit this exception to apply to your modifications.</span> <span class="comment">// If you do not wish that, delete this exception notice.</span> <span class="preprocessor">#include <<a class="code" href="socket_8h.html" title="Network addresses and sockets related classes.">cc++/socket.h</a>></span> <span class="preprocessor">#include <cstdlib></span> <span class="preprocessor">#ifdef CCXX_NAMESPACES</span> <span class="preprocessor"></span><span class="keyword">using namespace </span>std; <span class="keyword">using namespace </span>ost; <span class="preprocessor">#endif</span> <span class="preprocessor"></span> <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" title="TCP sockets are used for stream based connected sessions between two sockets.">TCPSocket</a> { <span class="keyword">protected</span>: <span class="keywordtype">bool</span> <a name="a1"></a><a class="code" href="classost_1_1_t_c_p_socket.html#a8ba3a6ecd27ab319e83bb6445828f7ef" title="A method to call in a derived TCPSocket class that is acting as a server when a connection...">onAccept</a>(<span class="keyword">const</span> <a name="a2"></a><a class="code" href="address_8h.html#a5eba36aa908d5a479c8ba7d0bc4512b1">InetHostAddress</a> &ia, <a name="a3"></a><a class="code" href="namespaceost.html#a3c74a1a40c359fd349f3e3e1b96ebfc7" title="Transport Protocol Ports.">tpport_t</a> port); <span class="keyword">public</span>: myTCPSocket(<a name="a4"></a><a class="code" href="address_8h.html#a029ebb5b43bb13b02c02f4de0a8f3c08">InetAddress</a> &ia); }; <span class="keyword">class </span>myTCPSession : <span class="keyword">public</span> <a name="_a5"></a><a class="code" href="classost_1_1_t_c_p_session.html" title="The TCP session is used to primarily to represent a client connection that can be...">TCPSession</a> { <span class="keyword">private</span>: <span class="keyword">static</span> <a name="_a6"></a><a class="code" href="classost_1_1_mutex.html" title="The Mutex class is used to protect a section of code so that at any given time only...">Mutex</a> mutex; <span class="keywordtype">void</span> <a name="a7"></a><a class="code" href="classost_1_1_thread.html#add7d339d94b8a1ed2b2b0324a95b7e74" title="All threads execute by deriving the Run method of Thread.">run</a>(<span class="keywordtype">void</span>); <span class="keywordtype">void</span> <span class="keyword">final</span>(void); <span class="keyword">public</span>: myTCPSession(<a class="code" href="classost_1_1_t_c_p_socket.html" title="TCP sockets are used for stream based connected sessions between two sockets.">TCPSocket</a> &server); <span class="keyword">static</span> <span class="keyword">volatile</span> <span class="keywordtype">int</span> count; }; myTCPSocket::myTCPSocket(<a class="code" href="address_8h.html#a029ebb5b43bb13b02c02f4de0a8f3c08">InetAddress</a> &ia) : <a class="code" href="classost_1_1_t_c_p_socket.html" title="TCP sockets are used for stream based connected sessions between two sockets.">TCPSocket</a>(ia, 4096) {} <span class="keywordtype">bool</span> <a class="code" href="classost_1_1_t_c_p_socket.html#a8ba3a6ecd27ab319e83bb6445828f7ef" title="A method to call in a derived TCPSocket class that is acting as a server when a connection...">myTCPSocket::onAccept</a>(<span class="keyword">const</span> <a class="code" href="address_8h.html#a5eba36aa908d5a479c8ba7d0bc4512b1">InetHostAddress</a> &ia, <a class="code" href="namespaceost.html#a3c74a1a40c359fd349f3e3e1b96ebfc7" title="Transport Protocol Ports.">tpport_t</a> port) { cout << <span class="stringliteral">"accepting from: "</span> << ia.getHostname() << <span class="stringliteral">":"</span> << port << endl;; <span class="keywordflow">return</span> <span class="keyword">true</span>; } <span class="keyword">volatile</span> <span class="keywordtype">int</span> myTCPSession::count = 0; <a class="code" href="classost_1_1_mutex.html" title="The Mutex class is used to protect a section of code so that at any given time only...">Mutex</a> myTCPSession::mutex; myTCPSession::myTCPSession(<a class="code" href="classost_1_1_t_c_p_socket.html" title="TCP sockets are used for stream based connected sessions between two sockets.">TCPSocket</a> &server) : <a class="code" href="classost_1_1_t_c_p_session.html" title="The TCP session is used to primarily to represent a client connection that can be...">TCPSession</a>(server) { cout << <span class="stringliteral">"creating session client object"</span> << endl; mutex.enterMutex(); ++count; mutex.leaveMutex(); <span class="comment">// unsetf(ios::binary);</span> } <span class="keywordtype">void</span> <a class="code" href="classost_1_1_thread.html#add7d339d94b8a1ed2b2b0324a95b7e74" title="All threads execute by deriving the Run method of Thread.">myTCPSession::run</a>(<span class="keywordtype">void</span>) { <a class="code" href="namespaceost.html#a3c74a1a40c359fd349f3e3e1b96ebfc7" title="Transport Protocol Ports.">tpport_t</a> port; <a name="_a8"></a><a class="code" href="classost_1_1_i_p_v4_address.html" title="The network name and address objects are all derived from a common IPV4Address base...">IPV4Address</a> addr = getLocal(&port); *tcp() << <span class="stringliteral">"welcome to "</span> << addr.<a name="a9"></a><a class="code" href="classost_1_1_i_p_v4_address.html#aaf68688cd2253cdcd313a12ab6177167" title="Provide a string representation of the value (Internet Address) held in the IPV4Address...">getHostname</a>() << <span class="stringliteral">" from socket "</span> << (int)so << endl; mutex.enterMutex(); *tcp() << <span class="stringliteral">"called from thread "</span> << count << endl; mutex.leaveMutex(); sleep(5000); *tcp() << <span class="stringliteral">"ending session"</span> << endl; } <span class="keywordtype">void</span> <a name="a10"></a><a class="code" href="classost_1_1_thread.html#a6fb521eb3d23c51925472a3fe0abf327" title="A thread that is self terminating, either by invoking exit() or leaving it&#39;s...">myTCPSession::final</a>(<span class="keywordtype">void</span>) { } <span class="keywordtype">int</span> main() { myTCPSession *tcp; <a name="a11"></a><a class="code" href="address_8h.html#a9dc0c24e79c7b6f5768fe61548395273">BroadcastAddress</a> addr; addr = <span class="stringliteral">"255.255.255.255"</span>; cout << <span class="stringliteral">"testing addr: "</span> << addr.<a class="code" href="classost_1_1_i_p_v4_address.html#aaf68688cd2253cdcd313a12ab6177167" title="Provide a string representation of the value (Internet Address) held in the IPV4Address...">getHostname</a>() << <span class="stringliteral">":"</span> << 4096 << endl; addr = <span class="stringliteral">"127.0.0.1"</span>; cout << <span class="stringliteral">"binding for: "</span> << addr.getHostname() << <span class="stringliteral">":"</span> << 4096 << endl; <span class="keywordflow">try</span> { myTCPSocket server(addr); <span class="keywordflow">while</span>(server.<a name="a12"></a><a class="code" href="classost_1_1_t_c_p_socket.html#a112e8235b317f15e15f6ea264fe81eb7" title="Used to wait for pending connection requests.">isPendingConnection</a>(30000)) { cout << <span class="stringliteral">"before create"</span> << endl; tcp = <span class="keyword">new</span> myTCPSession(server); cout << <span class="stringliteral">"after create"</span> << endl; tcp->detach(); } } <span class="keywordflow">catch</span>(<a name="_a13"></a><a class="code" href="classost_1_1_socket.html" title="The Socket is used as the base for all Internet protocol services under Common C++...">Socket</a> *socket) { <a class="code" href="namespaceost.html#a3c74a1a40c359fd349f3e3e1b96ebfc7" title="Transport Protocol Ports.">tpport_t</a> port; <span class="keywordtype">int</span> err = socket-><a name="a14"></a><a class="code" href="classost_1_1_socket.html#abfdaefd24888f2d51089bdb31894286e" title="Often used by a &quot;catch&quot; to fetch the last error of a thrown socket.">getErrorNumber</a>(); <a class="code" href="address_8h.html#a029ebb5b43bb13b02c02f4de0a8f3c08">InetAddress</a> saddr = (<a class="code" href="address_8h.html#a029ebb5b43bb13b02c02f4de0a8f3c08">InetAddress</a>)socket-><a name="a15"></a><a class="code" href="classost_1_1_socket.html#a6fd1dffdfe9606e56fba838559ff6d67">getPeer</a>(&port); cerr << <span class="stringliteral">"socket error "</span> << saddr.<a class="code" href="classost_1_1_i_p_v4_address.html#aaf68688cd2253cdcd313a12ab6177167" title="Provide a string representation of the value (Internet Address) held in the IPV4Address...">getHostname</a>() << <span class="stringliteral">":"</span> << port << <span class="stringliteral">" = "</span> << err << endl; <span class="keywordflow">if</span>(err == Socket::errBindingFailed) { cerr << <span class="stringliteral">"bind failed; port busy"</span> << endl; ::exit(-1); } <span class="keywordflow">else</span> cerr << <span class="stringliteral">"client socket failed"</span> << endl; } cout << <span class="stringliteral">"timeout after 30 seconds inactivity, exiting"</span> << endl; <span class="keywordflow">return</span> 0; } </pre></div> </div> <hr class="footer"/><address style="text-align: right;"><small>Generated on Fri Mar 12 10:07:38 2010 for GNU CommonC++ by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.3 </small></address> </body> </html>