bug-commoncpp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Reading from a tcpstream


From: Jon Wilson
Subject: Reading from a tcpstream
Date: Thu, 10 Apr 2003 16:07:08 +0100
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.2.1) Gecko/20021130

I have the following code...
However, when I try and use the inserter for the tcpstream it segfaults, and if I try to cast first, I get an error code of 1. Which if I am right means errCreateFailed. I create one WithdrawServer and one DepositServer, both running on different ports, on "localhost". Each of them runs as a thread creating another thread should a connection be stablished on the socket. I have to use a tcpstream* since I need control over the destructor for when the thread is terminated. The same is true for the socket. I cannot rely on the run methods completing for memory management since they may not complete should the threads be terminated from main. The serverthread and server classes are both virtual. The main creates a WithdrawServer, this waits on a connection and then creates a tcpstream from the TCPSocket. This is then passed to the constructor (as a pointer) of another thread which it starts for processing. However, even as soon as the stream is first created, calling the inserter segfaults! See astrisks. Is there another way to do this? The docs mentioned about the tcpstream being a...

more natural C++ "tcpstream" class for use by non-threaded applications. C++ "fstream" style tcpstream class.
This class behaves a lot more like fstream and similar classes.
Does that mean that it isn't thread safe? Is there another thread safe version???
Many Thanks.
Jon Wilson

class ServerThread:public Thread{
   public:
       ServerThread(tcpstream* tcp){
               cout << "\tTHREAD" << endl;
               this->tcp=tcp;
               setCancel(Thread::cancelImmediate);
               start();
       }
       ~ServerThread(){
               cout << "\t~THREAD" << endl;
               terminate();
               delete tcp;
       }
       virtual void run()=0;
   protected:
       tcpstream* tcp;
};
class WithdrawThread:public ServerThread{
   public:
       WithdrawThread(tcpstream* tcp):ServerThread(tcp){
           cout << (void*)tcp << endl;
       }
       void run();
};
class DepositThread:public ServerThread{
   public:
       DepositThread(tcpstream* tcp):ServerThread(tcp){}
       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;
               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:
       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;
};
class DepositServer:public Server{
   public:
       DepositServer(){
           this->port=Server::deposit_port;
       }
       void run();
   private:
       tpport_t port;
};
void DepositServer::run(){
   InetAddress addr(Server::host);
   sock=new TCPSocket(addr,port);
   while(true){
       if(sock->isPendingConnection()){
           cout << "D Connected" << endl;
           tcpstream* tcp=new tcpstream(*sock)
tcp << "HELLO" << endl; //*********************************************************** THIS WOULD CAUSE A SEGFAULT
           threads.push_back(new DepositThread(tcp));
           yield();
       }
   }
}
void WithdrawThread::run(){
   try{
       static int i=0;
       while(true){
           cout << (typeid(tcp)==typeid(tcpstream*)?"OK":"NOT OK") << endl;
           cout << (void*)tcp << endl;
           (tcpstream)*tcp << "W" << i << endl;
           cout << "W" << i++ << endl;
           yield();
       }
   }catch(Socket* s){
       if(s->getErrorNumber() == Socket::errResourceFailure){
               cerr << "bind failed; no resources" << endl;
       }
       if(s->getErrorNumber() == Socket::errBindingFailed){
               cerr << "bind failed; port busy" << endl;
       }
       cout << "ERR " << s->getErrorNumber() << endl;
   }
};





reply via email to

[Prev in Thread] Current Thread [Next in Thread]