OO Server. Thread problem
"Hey
I''m trying to write a multithreaded server app (for windows as of now), and I''ve run into a slight problem that I thought someome might be able to shed some light on.
I''ve got a base thread class, that''s something liket this:
(posted version is devoid of lots of error checking etc)
class BaseThread{
public:
void Begin();
virtual DWORD ThreadProc() = 0;
};
void BaseThread::Begin()
{
m_threadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)GenThreadProc, this, 0, (LPDWORD)&m_threadID);
}
DWORD WINAPI GenThreadProc(BaseThread* in)
{
return in->ThreadProc();
}
class InheritedServer: public BaseThread
{
InheritedServer();
DWORD ThreadProc();
SOCKET listeningSocket;
};
InheritedServer::InheritedServer()
{
listeningSocket = socket(... etc);
bind(); // etc
listen(); // etc
}
InheritedServer::ThreadProc()
{
while(true)
{
int result = accept(listeningSocket, ...);
if(result == INVALID_SOCKET)
{
// error
}
}
}
Because the initialization, binding etc is being done in 1 thread, and the accepting is being done in another, the accept fails every time. Now I can move the initialization into the other thread, but if I don''t want to, how can I pass the valid socket to the new thread so that it will work. Thanks.
-Vr
I''m not quite sure I understand what it is you''re doing here. From what you''ve said, it looks as though you''re creating several InheritedServer threads? This is a problem because each one will create its own listening socket in the constructor and try to bind it to the same interface and port. The call to bind() will fail in such a case.
What I would do is derive two classes from your BaseThread. A ServerThread and a ClientThread. The ServerThread would work just like your InheritedServer does now, but you would only create one of them. When you accept() a connection in your ThreadProc, create a new ClientThread and pass it the socket from the accept() call by way of the constructor. So it would look something like this:
You would want to keep a list or an array of those ClientThread pointers in your ServerThread, so that you could close them all down when you need to.
I hope that helps.
- Matt
What I would do is derive two classes from your BaseThread. A ServerThread and a ClientThread. The ServerThread would work just like your InheritedServer does now, but you would only create one of them. When you accept() a connection in your ThreadProc, create a new ClientThread and pass it the socket from the accept() call by way of the constructor. So it would look something like this:
ServerThread::ThreadProc(){ SOCKET clientSocket = accept(listenSocket, ...); if (clientSocket != INVALID_SOCKET) { ClientThread* pClientThread = new ClientThread(clientSocket); pClientThread->Begin(); ... } else { // error }}
You would want to keep a list or an array of those ClientThread pointers in your ServerThread, so that you could close them all down when you need to.
I hope that helps.
- Matt
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement