Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

A Complete FTP Server

, 30 May 2005
This article presents a fully functional implementation of a FTP server.
/****************************************************************/
/*																*/
/*  LISTSOCKET.CPP												*/
/*																*/
/*  Implementation of the Listen Socket.						*/
/*  The server listens for connections. When a new connection	*/
/*  is requested, the server accepts the connection and then	*/
/*  creates a connect thread to handle the connection.			*/
/*																*/
/*  Programmed by Pablo van der Meer							*/
/*  Copyright Pablo Software Solutions 2005						*/
/*	http://www.pablosoftwaresolutions.com						*/
/*																*/
/*  Last updated: May 28, 2005									*/
/*																*/
/****************************************************************/


#include "stdafx.h"
#include "FTPServerApp.h"	
#include "FTPServer.h"	
#include "ApplicationDlg.h"
#include "ListenSocket.h"
#include "ConnectThread.h"

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


CListenSocket::CListenSocket()
{
	m_pWndServer = NULL;
}

CListenSocket::~CListenSocket()
{
}


// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CListenSocket, CAsyncSocket)
	//{{AFX_MSG_MAP(CListenSocket)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif	// 0


/********************************************************************/
/*																	*/
/* Function name : OnAccept											*/		
/* Description   : Called by the framework to notify this listening	*/
/*				   socket that it can accept pending connection		*/
/*				   requests by calling the Accept member function.	*/
/*																	*/
/********************************************************************/
void CListenSocket::OnAccept(int nErrorCode) 
{
	// New connection is being established
	CSocket sockit;

	// Accept the connection using a temp CSocket object.
	Accept(sockit);

	// Create a thread to handle the connection. The thread is created suspended so that we can
	// set variables in CConnectThread before it starts executing.
	CConnectThread* pThread = (CConnectThread*)AfxBeginThread(RUNTIME_CLASS(CConnectThread), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
	if (!pThread)
	{
		sockit.Close();
		TRACE("Could not create thread\n");
		return;
	}

	CFTPServer *pWnd = (CFTPServer *)m_pWndServer;

    // since everything is successful, add the thread to our list
	pWnd->m_CriticalSection.Lock();
    pWnd->m_ThreadList.AddTail(pThread);
	pWnd->m_CriticalSection.Unlock();

	// save pointer
	pThread->m_pWndServer = m_pWndServer;

	// Pass the socket to the thread by passing the socket handle. You cannot pass
	// a CSocket object across threads.
	pThread->m_hSocket = sockit.Detach();

	// Now start the thread.
	pThread->ResumeThread();

	CAsyncSocket::OnAccept(nErrorCode);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Pablo van der Meer
Web Developer
Netherlands Netherlands
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 30 May 2005
Article Copyright 2002 by Pablo van der Meer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid