Click to See Complete Forum and Search --> : MFC Threads and Messages


rohini2018
October 8th, 2005, 02:16 PM
hi
i am developing a messenger in VC++ using CAsyncSocket class. problem is that multiple connections could not be established on a system. for that multithreading need to be used but my worker threads are not working.
I am new to socket as well as thread programming. please help.
I have derived two classes CSockList and CSockCon from CAsyncSocket. Coding in CSockList.cpp, CSockCon.cpp and CMessg1View.cpp is shown below:

///////////////////CSockCon.cpp/////////////////
#include "stdafx.h"
#include "messg1.h"
#include "SockCon.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

void OnCon();
void OnRecvd();

CSockCon::CSockCon(){}

CSockCon::~CSockCon(){}

#if 0
BEGIN_MESSAGE_MAP(CSockCon, CAsyncSocket)
//{{AFX_MSG_MAP(CSockCon)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0

/////////////////////////////////////////////////////////////////////////////
// CSockCon member functions

void CSockCon::OnConnect(int nErrorCode)
{
OnCon();
CAsyncSocket::OnConnect(nErrorCode);
}

void CSockCon::OnReceive(int nErrorCode)
{
OnRecvd();
CAsyncSocket::OnReceive(nErrorCode);
}


////////////////////CSockList.cpp////////////////////////
#include "stdafx.h"
#include "messg1.h"
#include "SockList.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


UINT ThreadAcc(LPVOID param);
//void OnAcc();
/////////////////////////////////////////////////////////////////////////////
// CSockList

CSockList::CSockList(){}

CSockList::~CSockList(){}

#if 0
BEGIN_MESSAGE_MAP(CSockList, CAsyncSocket)
//{{AFX_MSG_MAP(CSockList)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0

/////////////////////////////////////////////////////////////////////////////
// CSockList member functions

void CSockList::OnAccept(int nErrorCode)
{
AfxMessageBox("Accepting");
AfxBeginThread(ThreadAcc,0);
CAsyncSocket::OnAccept(nErrorCode);
}
////////////////////CMessg1View.cpp//////////////////////////////////

#include "stdafx.h"
#include "messg1.h"
#include "Connect.h"

#include "messg1Doc.h"
#include "messg1View.h"
#include "SockList.h"
#include "SockCon.h"
#include "Messg.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CSockList *sockl,*sockl2,*sockl3;
CSockCon *sockc,*sockc2;
CPtrList SockL;
CMessg *Mes1;

/////////////////////////////////////////////////////////////////////////////
// CMessg1View

IMPLEMENT_DYNCREATE(CMessg1View, CFormView)

BEGIN_MESSAGE_MAP(CMessg1View, CFormView)
//{{AFX_MSG_MAP(CMessg1View)
ON_COMMAND(ID_FILE_NewC, OnFILENewC)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMessg1View construction/destruction

CMessg1View::CMessg1View()
: CFormView(CMessg1View::IDD)
{
//{{AFX_DATA_INIT(CMessg1View)
//}}AFX_DATA_INIT
// TODO: add construction code here

}

CMessg1View::~CMessg1View()
{
}

void CMessg1View::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMessg1View)
//}}AFX_DATA_MAP
}

BOOL CMessg1View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs

return CFormView::PreCreateWindow(cs);
}

UINT ThreadSt(LPVOID param)
{
if(sockl->Create(4000)==0)
{
AfxMessageBox("Error in Creating Socket1");
}
else
{
sockl->Listen();
AfxMessageBox("Listening....Socket1");
}
AfxEndThread(1);
return 0;
}
UINT ThreadSt2(LPVOID param)
{
if(sockl2->Create(5000)==0)
{
AfxMessageBox("Error in Creating Socketc");
}
else
{
sockl2->Listen();
AfxMessageBox("Listening....Socketc");
}
AfxEndThread(1);
return 0;
}

void CMessg1View::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
sockl=new CSockList;
sockl2=new CSockList;
sockc2=new CSockCon;
sockc=new CSockCon;
Mes1=new CMessg;
AfxBeginThread(ThreadSt2,0);
AfxBeginThread(ThreadSt,0);
}

/////////////////////////////////////////////////////////////////////////////
// CMessg1View diagnostics

#ifdef _DEBUG
void CMessg1View::AssertValid() const
{
CFormView::AssertValid();
}

void CMessg1View::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}

CMessg1Doc* CMessg1View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMessg1Doc)));
return (CMessg1Doc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMessg1View message handlers


UINT ThreadAcc(LPVOID param)
{
if(sockl2->m_hSocket==INVALID_SOCKET)
{
if(!(sockl->Accept(*sockl3)))
{
AfxMessageBox("Could not accept the connection");
}
else
{
AfxMessageBox("Connection Accepted");
}
}
else if(sockl->m_hSocket==INVALID_SOCKET)
{
if(!(sockl2->Accept(*sockl3)))
{
AfxMessageBox("Could not accept the connection");
}
else
{
AfxMessageBox("Connection Accepted");
}
}
return 0;
}

void CMessg1View::OnFILENewC()
{
CConnect cond;
if(cond.DoModal()==IDOK)
{
sockc2->Create(3000);
sockc2->Connect(cond.m_CName,4000);
}
}
void OnCon()
{
AfxMessageBox("Connected");
}

void OnRecvd()
{
char *buf=new char[1024];
int irecd;
CString strd;
irecd=sockc->Receive(buf,1023);
if(irecd==SOCKET_ERROR)
{
AfxMessageBox("Error in receiving Message");
}
else
{
char temp[6];
CString SendN,tempstr;
UINT SendP;
int retval;
for(int i=0;i<=4;i++)
{
temp[i]=buf[i];
}
temp[i]='\0';
if(strcpy(temp,"File:")==0)
{
sockc->GetPeerName(SendN,SendP);
tempstr=SendN+" is trying to send you a "+buf+" Accept it?";
retval=AfxMessageBox(tempstr,MB_YESNO);
}
else
{
buf[irecd]=NULL;
strd=buf;
Mes1->m_RecdList.AddString(strd);
}
}
}

void CMessg1View::OnDestroy()
{
CFormView::OnDestroy();

// TODO: Add your message handler code here
delete sockl;
delete sockl2;
delete sockc;
delete sockc2;
}

MikeAThon
October 8th, 2005, 02:35 PM
Rohini,

You have made exactly three posts on CodeGuru, and each one of them inappropriately hijacks an existing thread, by asking an off-topic question that's completely irrelevant to the original question. In one case the original question was nearly one year old:

http://www.codeguru.com/forum/showthread.php?t=317847&p=1243520
http://www.codeguru.com/forum/showthread.php?t=358311&p=1247153

This will be the third time that you're being advised to start a new thread.

Please take this advice both for the good of the board (so that we can keep related questions together with their answers) and for your own good (since very few people will bother to answer off-topic questions in a hijacked thread).

If you are having difficulty in learning how to start a new thread, then please read the board FAQ at http://www.codeguru.com/forum/faq.php?faq=vb_faq#faq_vb_board_usage

And please use [ code ] tags when posting code.

Andreas Masur
October 9th, 2005, 11:19 AM
[ Split thread ]


rohini2018,

Please do not hijack other threads and in order to post your questions since this is a violation of the Acceptable Use Policy (http://www.jupitermedia.com/corporate/privacy/aup.html). Rather create a separate new thread as already indicated.

Thank you very much.