#include <cstdlib>
#ifdef CCXX_NAMESPACES
using namespace std;
#endif
{
protected:
public:
};
{
private:
void run(void);
void final(void);
public:
static volatile int count;
};
{
cout << "accepting from: " << ia.getHostname() << ":" << port << endl;;
return true;
}
volatile int myTCPSession::count = 0;
Mutex myTCPSession::mutex;
myTCPSession::myTCPSession(
TCPSocket &server) :
{
cout << "creating session client object" << endl;
mutex.enterMutex();
++count;
mutex.leaveMutex();
}
void myTCPSession::run(void)
{
*tcp() <<
"welcome to " << addr.
getHostname() <<
" from socket " << (int)so << endl;
mutex.enterMutex();
*tcp() << "called from thread " << count << endl;
mutex.leaveMutex();
sleep(5000);
*tcp() << "ending session" << endl;
}
void myTCPSession::final(void)
{
}
int main()
{
myTCPSession *tcp;
addr = "255.255.255.255";
cout <<
"testing addr: " << addr.
getHostname() <<
":" << 4096 << endl;
addr = "127.0.0.1";
cout << "binding for: " << addr.getHostname() << ":" << 4096 << endl;
try {
myTCPSocket server(addr);
cout << "before create" << endl;
tcp = new myTCPSession(server);
cout << "after create" << endl;
tcp->detach();
}
}
cerr <<
"socket error " << saddr.
getHostname() <<
":" << port <<
" = " << err << endl;
if(err == Socket::errBindingFailed) {
cerr << "bind failed; port busy" << endl;
::exit(-1);
}
else
cerr << "client socket failed" << endl;
}
cout << "timeout after 30 seconds inactivity, exiting" << endl;
return 0;
}
#define InetAddress
Definition address.h:75
#define InetHostAddress
Definition address.h:76
#define BroadcastAddress
Definition address.h:81
The network name and address objects are all derived from a common IPV4Address base class.
Definition address.h:351
const char * getHostname(void) const
Provide a string representation of the value (Internet Address) held in the IPV4Address object.
The Mutex class is used to protect a section of code so that at any given time only a single thread c...
Definition thread.h:187
The Socket is used as the base for all Internet protocol services under Common C++.
Definition socket.h:120
Error getErrorNumber(void) const
Often used by a "catch" to fetch the last error of a thrown socket.
Definition socket.h:668
IPV4Host getPeer(tpport_t *port=NULL) const
Definition socket.h:523
The TCP session is used to primarily to represent a client connection that can be managed on a sepera...
Definition socket.h:1868
TCP sockets are used for stream based connected sessions between two sockets.
Definition socket.h:1396
bool isPendingConnection(timeout_t timeout=TIMEOUT_INF)
Used to wait for pending connection requests.
Definition socket.h:1480
unsigned short tpport_t
Transport Protocol Ports.
Definition address.h:86
Network addresses and sockets related classes.