Scott Bailey - 2000-09-05

This is a Debian 2.2 GNU/Linux distro.
Kernel is v2.4.0-test5.
Machine is SMP i32.
Using APE 1.2  (1.2.3?)

The following code is from the examples directory:

#include <APE/thread.h>
#include <APE/socket.h>

class myTCPSocket : public TCPSocket
{
protected:
        bool OnAccept(InetHostAddress &ia, short port);

public:
        myTCPSocket(InetAddress &ia);
};

class myTCPSession : public Semaphore, public TCPSession
{
private:
        void Run(void);
  void Final(void);

public:
        myTCPSession(TCPSocket &server);
        static int count;
};

myTCPSocket::myTCPSocket(InetAddress &ia) : TCPSocket(ia, 4096) {};

bool myTCPSocket::OnAccept(InetHostAddress &ia, short port)
{
        cout << "accepting from: " << ia.getHostname() << ":" << port << endl;;
        return true;
}

int myTCPSession::count = 0;

myTCPSession::myTCPSession(TCPSocket &server) :
Semaphore(0), TCPSession((Semaphore *)this, server)
{
        cout << "creating session client object" << endl;
        ++count;
        unsetf(ios::binary);
}

void myTCPSession::Run(void)
{
        InetAddress addr = getLocal();
        *tcp() << "welcome to " << addr.getHostname() << " from socket " << so << endl;
        *tcp() << "called from thread " << count << endl;
        Sleep(20000);
        *tcp() << "ending session" << endl;
}

void myTCPSession::Final(void)
{
  delete this;
}

int main()
{
        myTCPSession *tcp;
        InetAddress addr;
        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);

                while(server.isPending(30000))
                {
                        tcp = new myTCPSession(server);
                        tcp->Start();
                }
        }
        catch(Socket *socket)
        {
                short port;
                int err = socket->getErrorNumber();
                InetAddress saddr = (InetAddress)socket->getPeer(&port);
                cerr << "socket error " << saddr.getHostname() << ":" << port << " = " << err << endl;
                if(err == SOCKET_BINDING_FAILED)
                {
                        cerr << "bind failed; port busy" << endl;
                        exit(-1);
                }
                else
                        cerr << "client socket failed" << endl;
        }
        cout << "timeout after 30 seconds inactivity, exiting" << endl;
}

This code was compiled w/:

   g++ -lape -lapesock -lpthread tcpthread.cpp -o tcpthread

then executed:

  ./tcpthread

The problem is when I execute:

   telnet localhost 4096

I connect, but the program does not send the welcome message.  I am relatively certain that the Run method is not being executed.  If I convert the Run method to public, and start it manually, it does print the welcome message.  What is wrong?