CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2008
    Location
    Cebu, Philippines
    Posts
    19

    Question CASyncSocket used in Console App

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

  2. #2
    Join Date
    Nov 2008
    Location
    Cebu, Philippines
    Posts
    19

    Re: CASyncSocket used in Console App

    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

    Code:
    _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++ (

    Code:
    // 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;
    }

  3. #3
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: CASyncSocket used in Console App

    Quote Originally Posted by klambiguit
    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

  4. #4
    Join Date
    Nov 2008
    Location
    Cebu, Philippines
    Posts
    19

    Re: CASyncSocket used in Console App

    Thank you for the reply MikeAThon. appreciate it..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured