How to create the thread for the server while listening.. in order to accept more clients.. The main concept is to communicate the other client with the help of the server.. In other words, the server should accept many clients... How to do it with the help of the THREAD????
#include "stdafx.h"
#include "newsocketserver.h"
#include "ServerClass.h"
#include "MySocket.h"
#include "Npapi.h"
UINT ServerListenThread(LPVOID pParam)
{
CServerClass *pWnd=(CServerClass *)pParam;
while(1)
{
pWnd->OnBnClickedBlisten();
}
return 0;
}
IMPLEMENT_DYNAMIC(CServerClass, CDialog)
CServerClass::CServerClass(CWnd* pParent )
: CDialog(CServerClass::IDD, pParent)
{
}
CServerClass::~CServerClass()
{
}
void CServerClass::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_BLISTEN, m_blis);
DDX_Text(pDX, IDC_EMESS, m_emessage);
DDX_Control(pDX, IDC_LSENT, m_lsent);
}
BEGIN_MESSAGE_MAP(CServerClass, CDialog)
ON_BN_CLICKED(IDC_BCLOSE, &CServerClass::OnBnClickedBclose)
ON_BN_CLICKED(IDC_BSEND, &CServerClass::OnBnClickedBsend)
ON_MESSAGE(WM_THREAD_MSG,OnThreadMsg)
END_MESSAGE_MAP()
BOOL CServerClass::OnInitDialog()
{
for (int n = 0 ; n < 10 ; n++)
{
h=CreateThread(NULL, 0,(LPTHREAD_START_ROUTINE)ServerListenThread,0,0,NULL);
}
CDialog::OnInitDialog();
UpdateData(FALSE);
m_connect.SetParent(this);
m_client.SetParent(this);
CheckRadioButton(IDC_RADIO1,IDC_RADIO2,IDC_RADIO1);
gethostname(host,201);
CString str;
str=host;
bClient =false;
if(!bClient)
{
k=1;
OnRadio(false);
OnBnClickedBlisten();
}
else
{
k=0;
OnRadio(true);
OnBnClickedConnect();
}
SetDefID(IDC_BSEND);
return FALSE;
}
void CServerClass::OnBnClickedBclose()
{
DestroyWindow();
}
void CServerClass::OnBnClickedBlisten()
{
GetDlgItem(IDC_BLISTEN)->EnableWindow(FALSE);
GetDlgItem(IDC_RADIO1)->EnableWindow(FALSE);
GetDlgItem(IDC_RADIO2)->EnableWindow(FALSE);
GetDlgItem(IDC_BSEND)->EnableWindow(FALSE);
GetDlgItem(IDC_EMESS)->EnableWindow(FALSE);
m_connect.Create(2500);
m_connect.Listen();
}
void CServerClass::OnBnClickedConnect()
{
GetDlgItem(IDC_BLISTEN)->EnableWindow(FALSE);
GetDlgItem(IDC_RADIO1)->EnableWindow(FALSE);
GetDlgItem(IDC_RADIO2)->EnableWindow(FALSE);
GetDlgItem(IDC_BSEND)->EnableWindow(FALSE);
GetDlgItem(IDC_EMESS)->EnableWindow(FALSE);
m_client.Create();
m_client.Connect(_T("192.168.1.132"),2500);
}
void CServerClass::OnAccept()
{
sockaddr_in from;
int fromsize=sizeof(from);
m_connect.Accept(m_client,(struct sockaddr *)&from,&fromsize);
if(m_connect!=INVALID_SOCKET)
{
int nValue=1;
this->PostMessage(WM_THREAD_MSG,nValue);
}
GetDlgItem(IDC_BSEND)->EnableWindow(TRUE);
GetDlgItem(IDC_EMESS)->EnableWindow(TRUE);
GetDlgItem(IDC_BCLOSE)->EnableWindow(TRUE);
}
LRESULT CServerClass::OnThreadMsg(WPARAM wParam,LPARAM lParam)
{
return 0;
}
void CServerClass::OnBnClickedBsend()
{
int iLen;
int iSent;
UpdateData(TRUE);
gethostname(host,201);
CString str,user;
str=host;
TCHAR acUserName[100];
DWORD nUserName = sizeof(acUserName);
GetUserName(acUserName, &nUserName);
tempuser=acUserName;
if(m_emessage!="")
{
iLen=m_emessage.GetLength();
iSent=m_client.Send(m_emessage,iLen*2);
if(iSent==SOCKET_ERROR)
{
}
else
{
int checked=GetCheckedRadioButton(IDC_RADIO1,IDC_RADIO2);
if(checked==IDC_RADIO1)
{
m_lsent.AddString(_T("me:")+m_emessage);
}
else if(checked==IDC_RADIO2)
{
m_lsent.AddString(_T("me:")+m_emessage);
}
m_emessage.Empty();
UpdateData(FALSE);
}
}
}
void CServerClass::OnClose()
{
}
void CServerClass::OnSend()
{
}
void CServerClass::OnReceive()
{
CString str,user;
gethostname(host,201);
str=host;
TCHAR acUserName[100];
DWORD nUserName = sizeof(acUserName);
GetUserName(acUserName, &nUserName);
user=acUserName;
WCHAR *pBuff=new WCHAR[100];
int iBuffSize=99;
int Rcvd;
Rcvd = m_client.Receive(pBuff,100);
struct sockaddr_in peer;
int peer_len;
peer_len = sizeof(peer);
if (getpeername(m_client, (struct sockaddr*)&peer, &peer_len) == -1) {
perror("getpeername() failed");
return;
}
struct hostent *remoteHost;
remoteHost = gethostbyaddr((char *) &peer.sin_addr, 4, AF_INET);
CString strIp;
strIp = remoteHost->h_name;
if(Rcvd==SOCKET_ERROR)
{
}
else
{
pBuff[Rcvd]=NULL;
CString lvstrRecev(pBuff);
int nLength;
nLength = lvstrRecev.GetLength();
CString strTemp;
for(int i=0;i<nLength/2;i++)
{
strTemp += lvstrRecev.GetAt(i);
}
if(k==1)
{
m_lsent.AddString(strIp+_T(":")+strTemp);
user="";
}
else if(k==0)
{
TCHAR acUserName[100];
DWORD nUserName = sizeof(acUserName);
GetUserName(acUserName,&nUserName);
tempuser=acUserName;
m_lsent.AddString(strIp+_T(":")+strTemp);
}
UpdateData(FALSE);
}
}
void CServerClass::OnRadio(bool bClient)
{
if(!bClient)
{
m_blis.SetWindowText(_T("LISTEN"));
}
else
{
m_blis.SetWindowText(_T("CONNECT"));
}
}
void CServerClass::OnConnect()
{
GetDlgItem(IDC_BSEND)->EnableWindow(TRUE);
GetDlgItem(IDC_EMESS)->EnableWindow(TRUE);
GetDlgItem(IDC_BCLOSE)->EnableWindow(TRUE);
}
DO ANYONE THERE FOR ME???