Click to See Complete Forum and Search --> : How do I start more than 1 thread ?


BlackSun
December 16th, 2004, 04:22 AM
So, this is what my app is supposed to do...
It's a regular client-server thingy, with one server and at most 4 other people connecting. This I've planned to do with the app running in one window and then a thread doing all the netowrking things, which worked quite nice, until I came up with this great idea, that the server should also be running a client, that way, there should be no special cases, the server just does it's thing and the client does his things. This would mean 3 threads in the server and 2 in the clients.

Of course, I don't get it to work, as soon as the client is started the server seems to shut down, why is that ?

heres my code ....

CWinThread *m_pServerThread;
CWinThread *m_pClientThread;

// The serverthread is started like this ...
m_pServerThread = AfxBeginThread(Server, this);

UINT CTestDlg::Server(LPVOID pParam)
{
// Get a link to the main window ...
CKommTestDlg* pDlg = (CTestDlg*) pParam;

// Get the sockets in order ...
// Some socket stuff going on...

// Start the own client ...
Clientstart(pDlg);

// Accept connections from the own client
// Do the accept stuff ....

MessageBox("I'm here !!"); // This never shows ...

while (true)
{
// Some really good stuff here ...
}

}

// The clientstart starts the client like this
m_pClientThread = AfxBeginThread(Client, this);

UINT CTestDlg::Client(LPVOID pParam)
{
// Link to main window
CKommTestDlg* pDlg = (CKommTestDlg*) pParam;
// Make all the connections, connect to server

while (true)
{
// I do get in here ...
}
return 0;
}


So, is it something with the way I start the threads?, cause If I run the client and server by themselves it works, so the networking stuff should be fine.
Or can't I have a program sonnect to itself? (network question maybe, nut you gurus seem to know it all :) )

ahoodin
December 16th, 2004, 09:30 AM
You can start threads until you run out of memory.

Of course, I don't get it to work, as soon as the client is started the server seems to shut down, why is that ?

Sounds to me like there is an exception.

When an exception happens in your code, the member function/function that calls that code ends. If that is a thread, that thread ends. Have you debugged? Check the output window for the message that occurs when the server ends. That is your first step.

Or can't I have a program sonnect to itself?

Honestly, you can do what you want, although in order to maintain sanity, you should keep the client and server instance seperate.

So if you have an executible that is running, that should explicitly be the client while another instance of your program should be the server.

ahoodin