multithreaded TCP Server Application(MFC)
-
I am writing a multithreaded TCP Server Application(MFC). I have written simple TCP server earlier n it worked fine. The tutorial I used told that for multithreaded server I have to use more than one Socket variable and It also told that the server should spin individual thread for every client. But I think I am writing wrong code. Where am I going wrong? any suggestions please. My Code looks like this:-
// Declarations of functions and variables.
class CsockDlg : public CDialog
{
// Construction
public:
CsockDlg(CWnd* pParent = NULL); // standard constructor// Dialog Data
.. .
private:
CMySocket m_sListenSocket;
CMySocket m_sConnectSocket;
public:
void OnAccept(void);
void OnConnect(void);
void OnClose(void);
void OnReceive(void);
void OnSend(void);
static UINT ThreadServer(LPVOID pParam);
void TS(void);
};// Definitions of functions of CsockDlg Class.
BOOL CsockDlg::OnInitDialog()
{
CDialog::OnInitDialog();// Add "About..." menu item to system menu. // IDM\_ABOUTBOX must be in the system command range. ASSERT((IDM\_ABOUTBOX & 0xFFF0) == IDM\_ABOUTBOX); . . . // TODO: Add extra initialization here int m\_iPort = 4000; // Default port for listening connections m\_sListenSocket.Create(m\_iPort); // Create listening socket m\_sListenSocket.Listen(); // Start listening on that port return TRUE; // return TRUE unless you set the focus to a control
}
void CsockDlg::OnAccept(void)
{
//m_sListenSocket.Accept(m_sConnectSocket);
AfxBeginThread(ThreadServer, this); // Spin thread for every client
}UINT CsockDlg::ThreadServer(LPVOID pParam)
{
CsockDlg* O = (CsockDlg*)pParam;
CMySocket c;
O->m_sListenSocket.Accept(c);
return 0;
}Future Lies in Present. Manmohan Bishnoi
-
I am writing a multithreaded TCP Server Application(MFC). I have written simple TCP server earlier n it worked fine. The tutorial I used told that for multithreaded server I have to use more than one Socket variable and It also told that the server should spin individual thread for every client. But I think I am writing wrong code. Where am I going wrong? any suggestions please. My Code looks like this:-
// Declarations of functions and variables.
class CsockDlg : public CDialog
{
// Construction
public:
CsockDlg(CWnd* pParent = NULL); // standard constructor// Dialog Data
.. .
private:
CMySocket m_sListenSocket;
CMySocket m_sConnectSocket;
public:
void OnAccept(void);
void OnConnect(void);
void OnClose(void);
void OnReceive(void);
void OnSend(void);
static UINT ThreadServer(LPVOID pParam);
void TS(void);
};// Definitions of functions of CsockDlg Class.
BOOL CsockDlg::OnInitDialog()
{
CDialog::OnInitDialog();// Add "About..." menu item to system menu. // IDM\_ABOUTBOX must be in the system command range. ASSERT((IDM\_ABOUTBOX & 0xFFF0) == IDM\_ABOUTBOX); . . . // TODO: Add extra initialization here int m\_iPort = 4000; // Default port for listening connections m\_sListenSocket.Create(m\_iPort); // Create listening socket m\_sListenSocket.Listen(); // Start listening on that port return TRUE; // return TRUE unless you set the focus to a control
}
void CsockDlg::OnAccept(void)
{
//m_sListenSocket.Accept(m_sConnectSocket);
AfxBeginThread(ThreadServer, this); // Spin thread for every client
}UINT CsockDlg::ThreadServer(LPVOID pParam)
{
CsockDlg* O = (CsockDlg*)pParam;
CMySocket c;
O->m_sListenSocket.Accept(c);
return 0;
}Future Lies in Present. Manmohan Bishnoi
Question: Where did you get that example from? There are plenty of examples out there that can help setting up a working networking application (with or without multi threading). For some examples using sockets see Winsock FAQ[^]. /M
Future lies in the future.
-
Question: Where did you get that example from? There are plenty of examples out there that can help setting up a working networking application (with or without multi threading). For some examples using sockets see Winsock FAQ[^]. /M
Future lies in the future.
Tutorial was only for Simple TCP server. This is my experimental code.
Future Lies in Present. Manmohan Bishnoi
-
Tutorial was only for Simple TCP server. This is my experimental code.
Future Lies in Present. Manmohan Bishnoi
Please have a look at the link I posted, perhaps you can find something that fits your needs. If you want to write a GUI application with networking it could be worth to have a look at event-based examples (e.g. using MFC's
CAsyncSocket
) it saves you the hassle of using threads and locks. Btw, there are other socket libraries than MFC out there: * Alhem * Boost * SharkEngine * QT * and many others Hope it helps! :)The past lies in the future