bug-commoncpp
[Top][All Lists]
Advanced

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

Having problems with TCPSession


From: Jon Wilson
Subject: Having problems with TCPSession
Date: Fri, 21 Feb 2003 10:44:57 +0000
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.2.1) Gecko/20021130

I have written the code below, however, this is the problem I have. When I telnet at the same port and ip as the server, It constructs a ThreadOut object and both
"Creating"
and
"Created"
print
as well as "Constructor"
However, the tread never starts, The run() is never invoked despite a call to detach(). Nothing happens! If I call run() in the constructor, then it runs, but obviously finalize doesn't and this defeats the entire point of having threads! Have I missed something? I am running on MingW with gcc3.2 on winXP.
Many Thanks.
Jon Wilson

///////////////////////////////////////////////////////////////////////////////////////////MAIN.CPP
#include "server.h"
using namespace std;
using namespace ost;

int main(int argc, char *argv[]){
   if(argc!=2){
       cout << "Arguments wrong" << endl;
       exit(1);
   }
   cout << argv[1] << endl;
   TCPSocket *sock = new TCPSocket(argv[1], 9382);
   cout << "Streams open" << endl;
   while(true){
       cout << "WAITING" << endl;
       if (sock->isPendingConnection()){
           cout << "Creating" << endl;
           (new ThreadOut(*sock))->detach();
           cout << "Created" << endl;
       }
   }
   exit(0);
}
///////////////////////////////////////////////////////////////////////////////////////////SERVER.H
#include <cc++/socket.h>

using namespace std;
using namespace ost;

class ThreadOut : public TCPSession {
   public:
       ThreadOut(TCPSocket& s):TCPSession(s){
           cout << "Constrcuter" << endl;
       }
       void run();
       void final();
};
///////////////////////////////////////////////////////////////////////////////////////////SERVER.CPP
#include "server.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include <ctime>

using namespace std;
using namespace ost;
void ThreadOut::run(){
   cout << "Running" << endl;
   iostream* tcp=(iostream*)this;
   time_t seconds= time(NULL);
   int i=seconds;
   cout << "Constructing Filename" << endl;
   ostringstream istr;
   istr << i;
   string name=istr.str()+"out.png";
   cout << "NAME "<< name << endl;
   ofstream out(name.c_str(),ios::binary);
   while(tcp->good()){
       out.put(tcp->get());
   }
   cout << "Read complete, closing" << endl;
   out.close();
   delete tcp;
}
void ThreadOut::final(){
   cout << "Thread complete" << endl;
   delete this;
}






reply via email to

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