如何在聊天应用程序中创建线程 [英] How to create the thread in chat application

查看:71
本文介绍了如何在聊天应用程序中创建线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在侦听时为服务器创建线程..以便接受更多客户端..主要概念是在服务器的帮助下与其他客户端进行通信.换句话说,服务器应接受许多客户端...如何在THREAD的帮助下完成????


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????


// 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???

推荐答案

您应该创建监听线程.客户端连接后,应在侦听线程中创建当前通信的客户端线程,并等待下一个客户端连接.
我想是的.
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.


通过CP中的以下链接

为初学者使用C ++编程Windows TCP套接字 [
go through the below link in CP

Programming Windows TCP Sockets in C++ for the Beginner[^]


因此,请尝试:
聊天客户端服务器 [
So, please try:
Chat Client Server[^]


这篇关于如何在聊天应用程序中创建线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆