\section{tcpthread.cpp} \footnotesize\begin{verbatim}1 // Copyright (C) 1999-2001 Open Source Telecom Corporation. 2 // 3 // This program is free software; you can redistribute it and/or modify 4 // it under the terms of the GNU General Public License as published by 5 // the Free Software Foundation; either version 2 of the License, or 6 // (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU General Public License for more details. 12 // 13 // You should have received a copy of the GNU General Public License 14 // along with this program; if not, write to the Free Software 15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 16 // 17 // As a special exception to the GNU General Public License, permission is 18 // granted for additional uses of the text contained in its release 19 // of APE. 20 // 21 // The exception is that, if you link the APE library with other files 22 // to produce an executable, this does not by itself cause the 23 // resulting executable to be covered by the GNU General Public License. 24 // Your use of that executable is in no way restricted on account of 25 // linking the APE library code into it. 26 // 27 // This exception does not however invalidate any other reasons why 28 // the executable file might be covered by the GNU General Public License. 29 // 30 // This exception applies only to the code released under the 31 // name APE. If you copy code from other releases into a copy of 32 // APE, as the General Public License permits, the exception does 33 // not apply to the code that you add in this way. To avoid misleading 34 // anyone as to the status of such modified files, you must delete 35 // this exception notice from them. 36 // 37 // If you write modifications of your own for APE, it is your choice 38 // whether to permit this exception to apply to your modifications. 39 // If you do not wish that, delete this exception notice. 40 41 #include <cc++/socket.h> 42 #include <cstdlib> 43 44 #ifdef CCXX_NAMESPACES 45 using namespace std; 46 using namespace ost; 47 #endif 48 49 class myTCPSocket : public TCPSocket 50 { 51 protected: 52 bool onAccept(const InetHostAddress &ia, tpport_t port); 53 54 public: 55 myTCPSocket(InetAddress &ia); 56 }; 57 58 class myTCPSession : public TCPSession 59 { 60 private: 61 static Mutex mutex; 62 void run(void); 63 void final(void); 64 65 public: 66 myTCPSession(TCPSocket &server); 67 static volatile int count; 68 }; 69 70 myTCPSocket::myTCPSocket(InetAddress &ia) : TCPSocket(ia, 4096) {} 71 72 bool myTCPSocket::onAccept(const InetHostAddress &ia, tpport_t port) 73 { 74 cout << "accepting from: " << ia.getHostname() << ":" << port << endl;; 75 return true; 76 } 77 78 volatile int myTCPSession::count = 0; 79 80 Mutex myTCPSession::mutex; 81 82 myTCPSession::myTCPSession(TCPSocket &server) : 83 TCPSession(server) 84 { 85 cout << "creating session client object" << endl; 86 mutex.enterMutex(); 87 ++count; 88 mutex.leaveMutex(); 89 // unsetf(ios::binary); 90 } 91 92 void myTCPSession::run(void) 93 { 94 tpport_t port; 95 IPV4Address addr = getLocal(&port); 96 *tcp() << "welcome to " << addr.getHostname() << " from socket " << (int)so << endl; 97 mutex.enterMutex(); 98 *tcp() << "called from thread " << count << endl; 99 mutex.leaveMutex(); 100 sleep(5000); 101 *tcp() << "ending session" << endl; 102 } 103 104 void myTCPSession::final(void) 105 { 106 } 107 108 int main() 109 { 110 myTCPSession *tcp; 111 BroadcastAddress addr; 112 addr = "255.255.255.255"; 113 cout << "testing addr: " << addr.getHostname() << ":" << 4096 << endl; 114 addr = "127.0.0.1"; 115 cout << "binding for: " << addr.getHostname() << ":" << 4096 << endl; 116 117 try 118 { 119 myTCPSocket server(addr); 120 121 while(server.isPendingConnection(30000)) 122 { 123 cout << "before create" << endl; 124 tcp = new myTCPSession(server); 125 cout << "after create" << endl; 126 tcp->detach(); 127 } 128 } 129 catch(Socket *socket) 130 { 131 tpport_t port; 132 int err = socket->getErrorNumber(); 133 InetAddress saddr = (InetAddress)socket->getPeer(&port); 134 cerr << "socket error " << saddr.getHostname() << ":" << port << " = " << err << endl; 135 if(err == Socket::errBindingFailed) 136 { 137 cerr << "bind failed; port busy" << endl; 138 ::exit(-1); 139 } 140 else 141 cerr << "client socket failed" << endl; 142 } 143 cout << "timeout after 30 seconds inactivity, exiting" << endl; 144 return 0; 145 } 146 \end{verbatim} \normalsize