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:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
I almost always use the socket family classes as objects on heap, i.e.
TCPStream *mystream =
new TCPStream("localhost",6000)
I have tried using member heap objects already, it does not improve the situation. =(
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";
}