Click to See Complete Forum and Search --> : CASyncSocket used in Console App


klambiguit
November 5th, 2008, 01:32 AM
Hello All. Can CAsyncSocket/CSocket be used in a Console based application?
if yes, can anyone point me to a link/tutorial/page etc. that shows how this is done.. thanks a lot...

klambiguit
November 5th, 2008, 03:50 AM
I think it is possible, i tried implementing it but i was stuck in an assertion error. When i debug it, it points me to this line of code in AFXWIN1.INL


_AFXWIN_INLINE HINSTANCE AFXAPI AfxGetInstanceHandle()
{ ASSERT(afxCurrentInstanceHandle != NULL);


I have no idea what's wrong..
here's my code. please be gentle, this is my first time using CAsyncSocket, MFC and C++ :((


// CSocketServer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "CSocketServer.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define SERVER_PORT 20000
/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;


//// Public interface
// Ctor and dtor
CSocketServer::CSocketServer()
{

}
CSocketServer::~CSocketServer()
{
}

// Start server
bool CSocketServer::Start()
{
bool bRetVal;

bRetVal = StartListening();

return bRetVal;
}
bool CSocketServer::Init(const char* addr, unsigned int port)
{
bool bRetVal;

bRetVal = AfxSocketInit();

if (!bRetVal)
{
AfxMessageBox("AfxSocketInit() Failed!!");
return FALSE;
}

bRetVal = CSocketServer::Create(port);
return bRetVal;
}



//// Internal support functions
bool CSocketServer::StartListening()
{
bool bRetVal = true;
bRetVal = CSocketServer::Listen();

return bRetVal;
}
void CSocketServer::SendEcho()
{
}
void CSocketServer::ReadReply()
{
}
void CSocketServer::CloseSocket()
{
}
bool CSocketServer::OnAsyncError(int nErrorCode, const char* pcFunc)
{
bool bRetVal = true;
return bRetVal;
}

// Overrides of CAsyncSocket::On* functions
void CSocketServer::OnAccept(int nErrorCode)
{
CSocketServer::Accept(oClientSock);
CAsyncSocket::OnAccept(nErrorCode);
}
void CSocketServer::OnClose(int nErrorCode)
{

CAsyncSocket::OnClose(nErrorCode);
}
void CSocketServer::OnReceive(int nErrorCode)
{
AfxMessageBox("OnReceive Event!!");
CAsyncSocket::OnReceive(nErrorCode);
}
void CSocketServer::OnSend(int nErrorCode)
{
AfxMessageBox("OnSend Event!!");
CAsyncSocket::OnSend(nErrorCode);
}

int main()
{
CSocketServer oCSocketServer;
oCSocketServer.Init(NULL, SERVER_PORT);
oCSocketServer.Start();

return 0;
}

MikeAThon
November 5th, 2008, 10:33 AM
Hello All. Can CAsyncSocket/CSocket be used in a Console based application?
if yes, can anyone point me to a link/tutorial/page etc. that shows how this is done.. thanks a lot...
No, it is not possible. Don't try to make it work, as you will be wasting your time.

Here's why: As you know, CAsyncSocket provides your program with asynchronous notifications of socket activity. The way it provides asynchronous notifications is by sending a message to a hidden window. A console application does not have a message pump, so the messages cannot get through.
please be gentle, this is my first time using CAsyncSocket, MFC and C++
If this is the first time using MFC, then it's generally wrong not to use the Wizards that are provided by the IDE. Do not try to side-step these Wizards. If you think you need to, because you think that the Wizards don't get you to where you want to go, then you probably can't get there anyway.

Mike

klambiguit
November 6th, 2008, 02:15 AM
Thank you for the reply MikeAThon. appreciate it.. :)