[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Passing a TCPSocket* to construct a TCPStream.
From: |
Jon Wilson |
Subject: |
Passing a TCPSocket* to construct a TCPStream. |
Date: |
Fri, 11 Apr 2003 15:11:10 +0100 |
User-agent: |
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.2.1) Gecko/20021130 |
I think I have narrowed down the problem I was having.
The constructor for the TCPStream requires a TCPSocket& passed by
reference. However, I need to use a TCPSocket* since as I terminate a
thread I cannot guarantee that the run() method completes and hence the
socket goes out of scope and gets deleted. For that reason, I need to
use a pointed type (TCPSocket*) and handle the "delete" myself in the
destructor. However, I need to use this to create a TCPStream. Is it
possible to do this???
Server::host="localhost";
InetAddress* addr=new InetAddress(Server::host);
TCPSocket* sock=new TCPSocket(*addr,port);
TCPStream* tcp=new TCPStream(*sock);
My slightly more complex code is as follows...
But I am pretty sure that this is the problem, is there another way I
should do things?
class ServerThread:public TCPSession{
public:
ServerThread(TCPSocket& sock):TCPSession(sock){
cout << "\tTHREAD" << endl;
setCancel(Thread::cancelImmediate);
start();
}
~ServerThread(){
cout << "\t~THREAD" << endl;
terminate();
}
virtual void run()=0;
protected:
};
class WithdrawThread:public ServerThread{
public:
WithdrawThread(TCPSocket& sock):ServerThread(sock){
cout << "\tWithdrawThread" << endl;
}
void run();
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Server:public Thread{
public:
list<ServerThread*> threads;
Server(){
cout << "SERVER" << endl;
setCancel(Thread::cancelImmediate);
start();
}
~Server(){
cout << "~SERVER" << endl;
terminate();
delete sock;
delete addr;
while(threads.size()){
delete threads.front();
threads.pop_front();
}
}
virtual void run()=0;
static void setHost(char* host){
Server::host=host;
};
static void setPorts(int withdraw,int deposit){
withdraw_port=withdraw;
deposit_port=deposit;
}
protected:
InetAddress* addr;
static char* host;
static tpport_t withdraw_port;
static tpport_t deposit_port;
TCPSocket* sock;
};
class WithdrawServer:public Server{
public:
WithdrawServer(){
this->port=Server::withdraw_port;
}
void run();
private:
tpport_t port;
};
void WithdrawServer::run(){
addr=new InetAddress(Server::host);
sock=new TCPSocket(*addr,port);
while(true){
if(sock->isPendingConnection()){
cout << "W Connected" << endl;
threads.push_back(new WithdrawThread(*sock));
cout << "Added to list" << endl;
yield();
}
}
}
It seems that constructing the WithdrawThread seems to make things segfault.
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Passing a TCPSocket* to construct a TCPStream.,
Jon Wilson <=