Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.33/5 (5 votes)
See more:
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????


C++
// ServerClass.cpp : implementation file
//

#include "stdafx.h"
#include "newsocketserver.h"
#include "ServerClass.h"
#include "MySocket.h"
#include "Npapi.h"

// CServerClass dialog
//
//UINT AcceptThread(LPVOID lp)
//{
//
//}
UINT ServerListenThread(LPVOID pParam)
{
	CServerClass *pWnd=(CServerClass *)pParam;
	while(1)
	{		
		pWnd->OnBnClickedBlisten();
	}
	
	return 0;
}

IMPLEMENT_DYNAMIC(CServerClass, CDialog)

CServerClass::CServerClass(CWnd* pParent /*=NULL*/)
	: 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_BLISTEN, &CServerClass::OnBnClickedBlisten)
	ON_BN_CLICKED(IDC_BSEND, &CServerClass::OnBnClickedBsend)
	ON_MESSAGE(WM_THREAD_MSG,OnThreadMsg)
	//ON_BN_CLICKED(IDC_RADIO1, &CServerClass::OnRadio)
	//ON_BN_CLICKED(IDC_RADIO2, &CServerClass::OnRadio)	
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);
	//OnRadio();
	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);
//	AfxBeginThread(ServerListenThread,(LPVOID)this);	
	//SetTimer(100, 6000, NULL);
	return FALSE;
}



// CServerClass message handlers
void CServerClass::OnBnClickedBclose()
{
	// TODO: Add your control notification handler code here
	 
	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);
	}
	
	//m_connect.Accept(m_client);
	

	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()
{
	// TODO: Add your control notification handler code here	
	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???
Posted
Updated 4-Jan-11 21:45pm
v5
Comments
[no name] 5-Jan-11 3:27am    
You should not create 10 threads. One thread should be created.
Dalek Dave 5-Jan-11 3:46am    
Edited for Code Block, but you may need to check what you have written.

You should create listen thread. When client connected, in listen thread you should create communication current client thread and wait for next client connection.
I think so.
 
Share this answer
 
v2
Comments
Gokulnath007 5-Jan-11 2:47am    
yeah... how to create that listen thread..
[no name] 5-Jan-11 2:54am    
Creates a thread.


uintptr_t _beginthread(
void( *start_address )( void * ),
unsigned stack_size,
void *arglist
);
uintptr_t _beginthreadex(
void *security,
unsigned stack_size,
unsigned ( *start_address )( void * ),
void *arglist,
unsigned initflag,
unsigned *thrdaddr
);
[no name] 5-Jan-11 2:58am    
You should develop your communication protocol:
server: wait request cliend: send request to get data (something like in http protocol GET FILE file1.cpp)
server: send file client: receive file
server: wait request client: send request to put message to outher client (SEND MESSAGE "hello all!")
server: wait request
Gokulnath007 5-Jan-11 3:05am    
How it is possible in the Dialog-based application. in vc++ MFC
[no name] 5-Jan-11 3:16am    
First you create listen thread.
It uses while(1) loop.
Then you create communication thread, when client connected.
It used your protocol. In while(1) loop it waits requests.
You can give the pointer to your dialog class to any thread, so they can call your dialog functions.
What is the difference dialog or not dialog?
 
Share this answer
 
Comments
Gokulnath007 5-Jan-11 3:06am    
yeah.. it is helpful.. But it doesnt say anything about the threads, creation and existence...
Rajesh Anuhya 5-Jan-11 4:22am    
As far i Understood your question , you are looking for Async Tcp Socket..??
Gokulnath007 5-Jan-11 5:01am    
yeah.... but using thread. the server has to accept many clients..
So, please try:
Chat Client Server[^]
 
Share this answer
 
Comments
Gokulnath007 5-Jan-11 2:22am    
thank u.. but am developing only MFC Dialog based and not the console application..
Gokulnath007 5-Jan-11 2:31am    
Please look at my updated question which contains the code am using..and guide me.. thank u....
 
Share this answer
 
v2
Comments
Gokulnath007 5-Jan-11 2:13am    
Could u please send me that in VC++ 2008. Visual c++ .. and not visual C#.. Thank u advance...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900