Menu

Crashes when encapsulating CC++'s TCP socket

Help
Anonymous
2001-07-04
2001-07-06
  • Anonymous

    Anonymous - 2001-07-04

    I am having problems with the TCPSocket and tcpstream classes. It seems that I cannot create and initialize them within another class. I am starting out with the demo tcp.cpp (which itself works fine for me), and have tried in several different ways to encapsulate that code in a class of my own. Every time I do it the compiled program crashes on me when I connect to it with a test client. Here is exactly what I am doing:

    #include <socket.h>
    #include <IOSTREAM.h>

    class TestClass
    {
    public:
        TestClass();
    //    ~TestClass();
        tcpstream m_Stream;
        TCPSocket m_Socket;
        void Test();
    };

    TestClass::TestClass():
        m_Socket(InetAddress(), 5555)
    {

    }

    void TestClass::Test()
    {
        int i;
        try
        {
            while(m_Socket.isPendingConnection())
            {
                m_Stream.open(m_Socket);
    //          m_Stream.unsetf(ios::binary);
                m_Stream << "5welcome to " << "" << endl;
                if(m_Stream.isPending(SOCKET_PENDING_INPUT, 2000))
                {
                    m_Stream >> i;
                    m_Stream << "user entered " << i << endl;
                }
                m_Stream << "exiting now" << endl;
                m_Stream.close();
            }
        }
        catch(Socket *socket)
        {
            tpport_t port;
            InetAddress saddr = (InetAddress)socket->getPeer(&port);
            cerr << "socket error " << saddr << ":" << port << endl;
            cout << "error number " << socket->getErrorNumber() << endl;       
            if(socket->getErrorNumber() == SOCKET_RESOURCE_FAILURE)
            {
                cerr << "bind failed; no resources" << endl;
                exit(-1);
            }
            if(socket->getErrorNumber() == SOCKET_BINDING_FAILED)
            {
                cerr << "bind failed; port busy" << endl;
                exit(-1);
            }
        }
        cout << "timeout after 30 seconds inactivity, exiting" << endl;
    }

    int main()
    {
        TestClass test;
        test.Test();

        return 0;
    }

    Among the things I have already tried is putting all the code that currently is in TestClass::Test() in TestClass' constructor, but that crashed on connect as well. Essentially the problem always comes up when I create and use the CC++ objects as members of my own classes, while the same code works great when put directly in main() (which is unacceptable for my purposes).
    I would _really_ appreciate a little guidance with this. Encapsulation of the TCP socket and streaming classes must have been done before.
    Thanks in advance for any help.

     
    • Ville Vainio

      Ville Vainio - 2001-07-04

      I almost always use the socket family classes as objects on heap, i.e.
      TCPStream *mystream =
      new TCPStream("localhost",6000)

       
      • Anonymous

        Anonymous - 2001-07-05

        I have tried using member heap objects already, it does not improve the situation. =(

         
        • Ville Vainio

          Ville Vainio - 2001-07-06

          Use TCPStream instead of tcpstream.

          And make a simpler example program to see where it fails.

          TCPSocket serv("localhost",8100);
          while (1) {
            TCPStream *s = new TCPStream(*serv);
            puts("accepted one");
            (*s) << "Hello world";
          }

           

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.