00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include <cc++/socket.h>
00042 #include <cstdlib>
00043
00044 #ifdef CCXX_NAMESPACES
00045 using namespace std;
00046 using namespace ost;
00047 #endif
00048
00049 class myTCPSocket : public TCPSocket
00050 {
00051 protected:
00052 bool onAccept(const InetHostAddress &ia, tpport_t port);
00053
00054 public:
00055 myTCPSocket(InetAddress &ia);
00056 };
00057
00058 class myTCPSession : public TCPSession
00059 {
00060 private:
00061 static Mutex mutex;
00062 void run(void);
00063 void final(void);
00064
00065 public:
00066 myTCPSession(TCPSocket &server);
00067 static volatile int count;
00068 };
00069
00070 myTCPSocket::myTCPSocket(InetAddress &ia) : TCPSocket(ia, 4096) {}
00071
00072 bool myTCPSocket::onAccept(const InetHostAddress &ia, tpport_t port)
00073 {
00074 cout << "accepting from: " << ia.getHostname() << ":" << port << endl;;
00075 return true;
00076 }
00077
00078 volatile int myTCPSession::count = 0;
00079
00080 Mutex myTCPSession::mutex;
00081
00082 myTCPSession::myTCPSession(TCPSocket &server) :
00083 TCPSession(server)
00084 {
00085 cout << "creating session client object" << endl;
00086 mutex.enterMutex();
00087 ++count;
00088 mutex.leaveMutex();
00089
00090 }
00091
00092 void myTCPSession::run(void)
00093 {
00094 tpport_t port;
00095 IPV4Address addr = getLocal(&port);
00096 *tcp() << "welcome to " << addr.getHostname() << " from socket " << (int)so << endl;
00097 mutex.enterMutex();
00098 *tcp() << "called from thread " << count << endl;
00099 mutex.leaveMutex();
00100 sleep(5000);
00101 *tcp() << "ending session" << endl;
00102 }
00103
00104 void myTCPSession::final(void)
00105 {
00106 }
00107
00108 int main()
00109 {
00110 myTCPSession *tcp;
00111 BroadcastAddress addr;
00112 addr = "255.255.255.255";
00113 cout << "testing addr: " << addr.getHostname() << ":" << 4096 << endl;
00114 addr = "127.0.0.1";
00115 cout << "binding for: " << addr.getHostname() << ":" << 4096 << endl;
00116
00117 try {
00118 myTCPSocket server(addr);
00119
00120 while(server.isPendingConnection(30000)) {
00121 cout << "before create" << endl;
00122 tcp = new myTCPSession(server);
00123 cout << "after create" << endl;
00124 tcp->detach();
00125 }
00126 }
00127 catch(Socket *socket) {
00128 tpport_t port;
00129 int err = socket->getErrorNumber();
00130 InetAddress saddr = (InetAddress)socket->getPeer(&port);
00131 cerr << "socket error " << saddr.getHostname() << ":" << port << " = " << err << endl;
00132 if(err == Socket::errBindingFailed) {
00133 cerr << "bind failed; port busy" << endl;
00134 ::exit(-1);
00135 }
00136 else
00137 cerr << "client socket failed" << endl;
00138 }
00139 cout << "timeout after 30 seconds inactivity, exiting" << endl;
00140 return 0;
00141 }
00142