Click to See Complete Forum and Search --> : multithreaded server


shahzadalam
April 10th, 1999, 05:40 AM
Sir what could be wrong there in this code
I want to develop a multithreaded server that connects to multiple clients
and sends them a proper message.
Now I am using the AfxBeginThread(....) .
I have declared two objects of CSocket
1. CSocket m_sListenSocket(); for listening in the header file of newServerDlg.h
2.and the other in the cpp file my main dialog which is as

// newServerDlg.cpp : implementation file
.......
......
CSocket m_sConnectSocket;
.....
......
void CNewServerDlg::OnButStartServer()
{
// TODO: Add your control notification handler code here
m_sListenSocket.Create(25);

BOOL Let_me_Listen = TRUE;

while (Let_me_Listen == TRUE)
{

m_sListenSocket.Listen();

m_sListenSocket.Accept(m_sConnectSocket);

AfxMessageBox("accepted");

CWinThread* newThread = AfxBeginThread(threadfunction, GetSafeHwnd(), THREAD_PRIORITY_NORMAL);
}
}

UINT CNewServerDlg::threadfunction(LPVOID pParam)

{

CString sTemp = "220 192.168.0.14 Service Ready";
m_sConnectSocket.Send( (LPCTSTR)sTemp, sTemp.GetLength() );
m_sConnectSocket.Close();

return 0;
}

Now there is no compile error but when I run it and a client is connected to it it is accepted.
the message is sent to client but then three Degug Assertion Failed errors are there
1. sockcore.cpp line no 177
2.sockcore.cpp line no 178
3.sockcore.cpp line no 407

Jeff Dockman
April 10th, 1999, 12:14 PM
Where is your connect socket initialized? I see that you called Create on your listen socket, but was it called on the connect socket? The first 2 ASSERTs are triggered as a result of an invalid socket in CAsyncSocket::Accept

A side note, your notation could get a bit confusing. You use m_sConnect socket to represent what appears to be a global socket. It is generally accepted that globals are prepended with g_ and members are prepended with m_. Also, the 's' in a variable declared as 'm_sVariable' is generally used to indicate a member variable of type 'short'.