This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

| Pop3 Class Submitted by

This is a small class which is able to handle POP3 actions with Winsock 2. It´s an implementation of RFC 1939 (you´ll find more RFCs at http://www.rfc-editor.org/rfcxx00.html). A small test program for VC++ with work space is included. I´m publishing this code under the GPL. Hope it´s somehow useful to you! Greetings,
Andy

Currently browsing [ub_pop3_10.zip] (55,328 bytes) - [main.cpp] - (2,852 bytes)

    /******************************************************************
      (C) Copyright 2001 by UNITED BYTES

      web:  http://www.unitedbytes.de
            http://www.unitedbytes.org
    		http://www.unitedbytes.net
      mail: info@unitedbytes.de

      This program is free software; you can redistribute it and/or
      modify it under the terms of the GNU General Public License
      as published by the Free Software Foundation; either version 2
      of the License, or (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place - Suite 330,
      Boston, MA  02111-1307, USA.

      *****************************************************************

      Program:      POP3 Test-Program
      File:         main.cpp
      Author:       Andreas Loeffler
      Date:         Mon, 10.04.2001
      Version:      1.0
      Description:  Small test program for POP3 class

      -----------------------------------------------------------------

      Changes:		-
      Date:			-
      Description:	-

      *****************************************************************/

    #include "sources/cub_pop.h"

    // Header file for console handling
    #include <conio.h>

    void main ()
    {
    	CUB_popClient	  popClient;
    	CUB_popClientInfo popInfo;

    	//////////////////////////////////////////////////////////////////////////////////////////
    	// Fill out the popClient structure:
    	//
    	//		- m_cMailboxName = myname@mydomain.com
    	//		- m_cPassword    = mypassword
    	//////////////////////////////////////////////////////////////////////////////////////////

    	printf ("*** POP3 Test - by United Bytes ***\n\n");

    	// Get the mail box name
    	printf ("Enter account name: ");
    	scanf ("%s", popInfo.m_cMailboxName);

    	// Get the password
    	printf ("\nEnter account password: ");
    	scanf ("%s", popInfo.m_cPassword);

    	// Get the mail server address
    	printf ("\nEnter POP3 server address: ");
    	scanf ("%s", popInfo.m_cPopServer);

    	// Get the server port
    	printf ("\nEnter server port (default is 110): ");
    	scanf ("%ld", &popInfo.m_lServerPort);

    	// Connect to a mail server of your provider
    	if (!popClient.ConnectToPOP3 (&popInfo))
    	{
    		printf ("ERROR: %s\n", popClient.m_cErrorText);
    	}
    	else {
    		printf ("Connected!\n");

    		printf ("You have %ld new messages which have %ld bytes!\n", popClient.GetNumMessages (), popClient.GetSizeOfMessages());

    		while (true)
    		{
    			popClient.MessageManager ();

    			if (kbhit ())
    				break;
    		}

    		popClient.Shutdown ();
    	}
    }

Currently browsing [ub_pop3_10.zip] (55,328 bytes) - [Sources/cub_n_client.h] - (2,362 bytes)

    /******************************************************************
      (C) Copyright 2001 by UNITED BYTES

      web:  http://www.unitedbytes.de
            http://www.unitedbytes.org
    		http://www.unitedbytes.net
      mail: info@unitedbytes.de

      This program is free software; you can redistribute it and/or
      modify it under the terms of the GNU General Public License
      as published by the Free Software Foundation; either version 2
      of the License, or (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place - Suite 330,
      Boston, MA  02111-1307, USA.

      *****************************************************************

      Program:      ubNetwork
      File:         sources/cub_n_client.h
      Author:       Andreas Loeffler
      Date:         Mon, 10.04.2001
      Version:      1.0
      Description:  Client base class for internet applications

      -----------------------------------------------------------------

      Changes:		-
      Date:			-
      Description:	-

      *****************************************************************/

    #ifndef __CUB_NETWORK_CLIENT_H__
    #define __CUB_NETWORK_CLIENT_H__

    #include <stdio.h>

    #include "cub_n_socket.h"

    // Global network length defines
    #define CUB_NETWORK_MAXTEXT				255

    // Global network states
    #define CUB_NS_NOT_INITIALIZED			0
    #define CUB_NS_WINSOCK_INIT				1
    #define CUB_NS_SOCKET_CREATED			2
    #define CUB_NS_CONNECTED				3

    class CUB_Client
    {
    	public:
    							CUB_Client ();
    						   ~CUB_Client ();
    		bool				Initialize ();
    		bool				Shutdown		();
    		bool				Connect			();
    		bool				CloseConnection ();
    		bool				GetLocalIP      (char *cBuffer);
    		bool				Setup			(char *cServerIP, int iPort);
    		bool				IsConnected		();
    		bool				CreateSocket    ();

    		char				m_cErrorText [CUB_NETWORK_MAXTEXT];
    		CUB_Socket			m_Socket;
    		int					m_iInitLevel;

    	private:
    		char				m_cServerIP [CUB_NETWORK_MAXTEXT];
    		int					m_iServerPort;
    		int					m_iClientID;
    };

    #endif

Currently browsing [ub_pop3_10.zip] (55,328 bytes) - [Sources/cub_pop_baseclass.cpp] - (1,970 bytes)

    /******************************************************************
      (C) Copyright 2001 by UNITED BYTES

      web:  http://www.unitedbytes.de
            http://www.unitedbytes.org
    		http://www.unitedbytes.net
      mail: info@unitedbytes.de

      This program is free software; you can redistribute it and/or
      modify it under the terms of the GNU General Public License
      as published by the Free Software Foundation; either version 2
      of the License, or (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place - Suite 330,
      Boston, MA  02111-1307, USA.

      *****************************************************************

      Program:      ubNetwork
      File:         sources/cub_pop_baseclass.cpp
      Author:       Andreas Loeffler
      Date:         Mon, 10.04.2001
      Version:      1.0
      Description:  Base class for pop class

      -----------------------------------------------------------------

      Changes:		-
      Date:			-
      Description:	-

      *****************************************************************/

    #include "cub_pop_baseclass.h"

    CUB_popBaseClass::CUB_popBaseClass ()
    {
    	BaseShutdown ();
    }

    CUB_popBaseClass::~CUB_popBaseClass ()
    {
    	BaseShutdown ();
    }

    void CUB_popBaseClass::BaseShutdown ()
    {
    }

    bool CUB_popBaseClass::GetError (long lErrorNum, char *cErrorText)
    {
    	// Check for valid pointer
    	if (cErrorText == 0) {
    		lErrorNum = -99;
    	}
    	else {
    		// Get the error
    		switch (lErrorNum)
    		{
    			default:
    				lErrorNum = -99;
    				break;
    		}
    	}

    	if (lErrorNum == -99)
    		return false;

    	return true;
    }

Currently browsing [ub_pop3_10.zip] (55,328 bytes) - [Sources/cub_pop_baseclass.h] - (1,746 bytes)

    /******************************************************************
      (C) Copyright 2001 by UNITED BYTES

      web:  http://www.unitedbytes.de
            http://www.unitedbytes.org
    		http://www.unitedbytes.net
      mail: info@unitedbytes.de

      This program is free software; you can redistribute it and/or
      modify it under the terms of the GNU General Public License
      as published by the Free Software Foundation; either version 2
      of the License, or (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place - Suite 330,
      Boston, MA  02111-1307, USA.

      *****************************************************************

      Program:      ubNetwork
      File:         sources/cub_pop_baseclass.h
      Author:       Andreas Loeffler
      Date:         Mon, 10.04.2001
      Version:      1.0
      Description:  Header file for pop base class

      -----------------------------------------------------------------

      Changes:		-
      Date:			-
      Description:	-

      *****************************************************************/

    #ifndef __CUB_POP_BASECLASS_H__
    #define __CUB_POP_BASECLASS_H__

    #include "cub_pop_globals.h"

    class CUB_popBaseClass
    {
    	public:
    						CUB_popBaseClass ();
    					   ~CUB_popBaseClass ();
    		bool			GetError		 (long lErrorNum, char *cErrorText);
    		void			BaseShutdown     ();

    	private:
    };

    #endif

Currently browsing [ub_pop3_10.zip] (55,328 bytes) - [Sources/cub_pop.h] - (1,771 bytes)

    /******************************************************************
      (C) Copyright 2001 by UNITED BYTES

      web:  http://www.unitedbytes.de
            http://www.unitedbytes.org
    		http://www.unitedbytes.net
      mail: info@unitedbytes.de

      This program is free software; you can redistribute it and/or
      modify it under the terms of the GNU General Public License
      as published by the Free Software Foundation; either version 2
      of the License, or (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place - Suite 330,
      Boston, MA  02111-1307, USA.

      *****************************************************************

      Program:      ubNetwork
      File:         sources/cub_pop.h
      Author:       Andreas Loeffler
      Date:         Mon, 10.04.2001
      Version:      1.0
      Description:  Header file for pop class

      -----------------------------------------------------------------

      Changes:		-
      Date:			-
      Description:	-

      *****************************************************************/

    #ifndef __CUB_POP_H__
    #define __CUB_POP_H__

    // Exclude rarely-used stuff from windows headers
    #define WIN32_LEAN_AND_MEAN

    // Link with the needed lib files
    #pragma comment (lib, "wsock32.lib")

    // All header files
    #include "cub_pop_globals.h"
    #include "cub_pop_client.h"

    #include "cub_n_socket.h"
    #include "cub_n_client.h"

    #endif

Currently browsing [ub_pop3_10.zip] (55,328 bytes) - [Sources/cub_pop_client.h] - (2,397 bytes)

    /******************************************************************
      (C) Copyright 2001 by UNITED BYTES

      web:  http://www.unitedbytes.de
            http://www.unitedbytes.org
    		http://www.unitedbytes.net
      mail: info@unitedbytes.de

      This program is free software; you can redistribute it and/or
      modify it under the terms of the GNU General Public License
      as published by the Free Software Foundation; either version 2
      of the License, or (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place - Suite 330,
      Boston, MA  02111-1307, USA.

      *****************************************************************

      Program:      ubNetwork
      File:         sources/cub_pop_client.h
      Author:       Andreas Loeffler
      Date:         Mon, 10.04.2001
      Version:      1.0
      Description:  Header file for pop client implementation (RFC 1939)

      -----------------------------------------------------------------

      Changes:		-
      Date:			-
      Description:	-

      *****************************************************************/

    #ifndef __CUB_POP_CLIENT_H__
    #define __CUB_POP_CLIENT_H__

    #include "cub_n_client.h"
    #include "cub_pop_baseclass.h"

    struct CUB_popClientInfo
    {
    	char m_cMailboxName [CUB_POP_MAX_ARGUMENT];
    	char m_cPassword    [CUB_POP_MAX_ARGUMENT];
    	char m_cPopServer	[CUB_POP_MAX_ARGUMENT];
    	long m_lServerPort;
    };

    class CUB_popClient : public CUB_Client, CUB_popBaseClass
    {
    	public:
    							CUB_popClient ();
    						   ~CUB_popClient ();

    		bool				ConnectToPOP3		 (CUB_popClientInfo *pClientInfo);
    		void				Shutdown			 ();
    		long				ReceiveServerMessage (char *cBuffer, long lBufLen);
    		bool				SendServerMessage    (char *cBuffer, long lBufLen);
    		bool				SendCommand			 (char *cCommand);
    		bool				CommandValid		 (char *cCommand);
    		bool				MessageManager		 ();

    		long				GetNumMessages       ();
    		long				GetSizeOfMessages    ();

    	private:
    		long				m_lNumMessages;
    		long				m_lSizeOfMessages;

    };

    #endif

Currently browsing [ub_pop3_10.zip] (55,328 bytes) - [Sources/cub_pop_client.cpp] - (8,201 bytes)

    /******************************************************************
      (C) Copyright 2001 by UNITED BYTES

      web:  http://www.unitedbytes.de
            http://www.unitedbytes.org
    		http://www.unitedbytes.net
      mail: info@unitedbytes.de

      This program is free software; you can redistribute it and/or
      modify it under the terms of the GNU General Public License
      as published by the Free Software Foundation; either version 2
      of the License, or (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place - Suite 330,
      Boston, MA  02111-1307, USA.

      *****************************************************************

      Program:      ubNetwork
      File:         sources/cub_pop_client.cpp
      Author:       Andreas Loeffler
      Date:         Mon, 10.04.2001
      Version:      1.0
      Description:  Pop client implementation (RFC 1939)

      -----------------------------------------------------------------

      Changes:		-
      Date:			-
      Description:	-

      *****************************************************************/

    #include "cub_pop_client.h"

    CUB_popClient::CUB_popClient ()
    {
    	Shutdown ();
    }

    CUB_popClient::~CUB_popClient ()
    {
    	Shutdown ();
    }

    void CUB_popClient::Shutdown ()
    {
    	// Clear variables
    	m_lNumMessages    = 0;
    	m_lSizeOfMessages = 0;

    	// Send quit message
    	SendCommand ("QUIT");
    }

    bool CUB_popClient::ConnectToPOP3 (CUB_popClientInfo *pClientInfo)
    {
    	int  iError = 0;
    	char cBuffer [CUB_POP_MAX_MESSAGE];

    	// Check for valid data
    	if (!pClientInfo) {
    		sprintf (m_cErrorText, "CUB_popClient->ConnectToPOP3: Invalid connection parameters given!");
    		iError = 1;
    	}
    	else {
    		// Set up the parameters
    		if (!Setup (pClientInfo->m_cPopServer, pClientInfo->m_lServerPort)) {
    			iError = 1;
    		}
    		else {
    			// Initialize the client
    			if (!Initialize ()) {
    				iError = 1;
    			}
    			else {
    				// Connect to the server!
    				if (!Connect ()) {
    					iError = 1;
    				}
    			}
    		}
    	}

    	// Still no error occured?
    	if (iError == 0)
    	{
    		////////////////////////////////////////////////////////////////////////////////////////
    		// Send login information
    		////////////////////////////////////////////////////////////////////////////////////////

    		// Eat (=hide) welcome message
    		ReceiveServerMessage (cBuffer, CUB_POP_MAX_MESSAGE);

    		// Send mailbox name
    		sprintf (cBuffer, "USER %s", pClientInfo->m_cMailboxName);
    		SendCommand (cBuffer);

    		// Await response
    		ReceiveServerMessage (cBuffer, CUB_POP_MAX_MESSAGE);

    		if (!CommandValid (cBuffer)) {
    			sprintf (m_cErrorText, "User/Mailbox name not correct!");
    			iError = 1;
    		}
    		else {
    			// Send password
    			sprintf (cBuffer, "PASS %s", pClientInfo->m_cPassword);
    			SendCommand (cBuffer);

    			// Await response
    			ReceiveServerMessage (cBuffer, CUB_POP_MAX_MESSAGE);

    			if (!CommandValid (cBuffer)) {
    				sprintf (m_cErrorText, "Password name not correct or logged in too frequently!");
    				iError = 1;
    			}
    			else {
    				// Send request for statistics
    				SendCommand ("STAT");

    				// Read number and size of all messages in mailbox
    				ReceiveServerMessage (cBuffer, CUB_POP_MAX_MESSAGE);

    				if (!CommandValid (cBuffer)) {
    					sprintf (m_cErrorText, "Cannot retrieve number of mails!");
    					iError = 1;
    				}
    				else {
    					// Extract the numbers
    					sscanf (cBuffer, "+OK %ld %ld", &m_lNumMessages, &m_lSizeOfMessages);
    				}
    			}
    		}
    	}

    	if (iError != 0)
    		return false;

    	return true;
    }

    bool CUB_popClient::CommandValid (char *cCommand)
    {
    	////////////////////////////////////////////////////////////////////////////////////////
    	// Checks for +OK or -ERR at the beginning
    	////////////////////////////////////////////////////////////////////////////////////////

    	int iError = 0;
    	char cBuffer [16];

    	if (!cBuffer) {
    		sprintf (m_cErrorText, "CUB_popClient->CommandValid: Buffer pointer invalid or too small!");
    		iError = 1;
    	}
    	else {
    		// Clear buffer
    		memset (cBuffer, 0, sizeof (cBuffer));

    		// Get the +OK (maybe)
    		memcpy (cBuffer, cCommand, 3);

    		if (!strcmp (cBuffer, "+OK"))
    			return true;
    		else
    			return false;
    	}

    	if (iError != 0)
    		return false;

    	return true;
    }

    long CUB_popClient::ReceiveServerMessage (char *cBuffer, long lBufLen)
    {
    	////////////////////////////////////////////////////////////////////////////////////////
    	// Receives a message stream from the POP3 Server
    	////////////////////////////////////////////////////////////////////////////////////////

    	int		iError   = 0;
    	int		iReturn  = 0;
    	long	lCounter = 0;
    	char	cChar    = 0;

    	if ((!cBuffer) || lBufLen < CUB_POP_MAX_MESSAGE) {
    		sprintf (m_cErrorText, "CUB_popClient->ReceiveServerMessage: Buffer pointer invalid or too small!");
    		iError = 1;
    	}
    	else {
    		memset (cBuffer, 0, lBufLen);

    		if (m_iInitLevel < CUB_NS_CONNECTED) {
    			sprintf (m_cErrorText, "CUB_popClient->ReceiveServerMessage: Client not connected yet!");
    			iError = 1;
    		}
    	}

    	if (iError == 0)
    	{
    		while (true)
    		{
    			iReturn = m_Socket.Receive (&cChar, 1);

    			if (iReturn <= 0)
    				break;

    			cBuffer[lCounter] = cChar;

    			lCounter ++;

    			if (cChar == '\n')
    				break;
    		}
    	}

    	// Remove the CRLF from the buffer
    	cBuffer [lCounter - 1] = '\0';

    	if (iError != 0)
    		return 0;

    	return lCounter;
    }

    bool CUB_popClient::SendServerMessage (char *cBuffer, long lBufLen)
    {
    	int		iError    = 0;
    	long	lCounter  = 0;
    	long	lNumChars = 0;

    	if (!cBuffer) {
    		sprintf (m_cErrorText, "CUB_popClient->SendServerMessage: Buffer pointer invalid!");
    		iError = 1;
    	}
    	else {
    		if (m_iInitLevel < CUB_NS_CONNECTED) {
    			sprintf (m_cErrorText, "CUB_popClient->SendServerMessage: Client not connected yet!");
    			iError = 1;
    		}
    	}

    	// Set an CRLF at the end of the line
    	strcat (cBuffer, "\n");

    	// Count the chars in the string
    	lNumChars = strlen (cBuffer);

    	if (iError == 0)
    	{
    		while (lCounter < lNumChars)
    		{
    			lCounter += m_Socket.Send (cBuffer, lNumChars);
    		}
    	}

    	if (iError != 0)
    		return false;

    	return true;
    }

    bool CUB_popClient::SendCommand (char *cCommand)
    {
    	////////////////////////////////////////////////////////////////////////////////////////
    	// Send a command to the server
    	////////////////////////////////////////////////////////////////////////////////////////

    	int	 iError = 0;
    	char cBuffer [CUB_POP_MAX_MESSAGE];

    	if (!cBuffer) {
    		sprintf (m_cErrorText, "CUB_popClient->SendCommand: Buffer pointer invalid!");
    		iError = 1;
    	}
    	else {
    		if (m_iInitLevel < CUB_NS_CONNECTED) {
    			sprintf (m_cErrorText, "CUB_popClient->SendCommand: Client not connected yet!");
    			iError = 1;
    		}
    	}

    	if (iError == 0)
    	{
    		sprintf (cBuffer, "%s", cCommand);
    		return SendServerMessage (cBuffer, CUB_POP_MAX_MESSAGE);
    	}

    	return false;
    }

    bool CUB_popClient::MessageManager ()
    {
    	////////////////////////////////////////////////////////////////////////////////////////
    	// Scans all incoming messages
    	////////////////////////////////////////////////////////////////////////////////////////

    	int  iError = 0;
    	char cBuffer [CUB_POP_MAX_MESSAGE];

    	if (m_iInitLevel < CUB_NS_CONNECTED) {
    		sprintf (m_cErrorText, "CUB_popClient->Refresh: Client not connected yet!");
    		iError = 1;
    	}
    	else {
    		// Something found in buffer?
    		if (m_Socket.CanReceive ())
    		{
    			// Receive message from server
    			if (!ReceiveServerMessage (cBuffer, 512)) {
    				iError = 1;
    			}
    			else {
    				if (strcmp (cBuffer, ""))
    					printf ("%s\n", cBuffer);
    			}

    		}

    	}

    	return false;
    }

    long CUB_popClient::GetNumMessages ()
    {
    	return m_lNumMessages;
    }

    long CUB_popClient::GetSizeOfMessages ()
    {
    	return m_lSizeOfMessages;
    }

Currently browsing [ub_pop3_10.zip] (55,328 bytes) - [Sources/cub_pop_globals.h] - (2,097 bytes)

    /******************************************************************
      (C) Copyright 2001 by UNITED BYTES

      web:  http://www.unitedbytes.de
            http://www.unitedbytes.org
    		http://www.unitedbytes.net
      mail: info@unitedbytes.de

      This program is free software; you can redistribute it and/or
      modify it under the terms of the GNU General Public License
      as published by the Free Software Foundation; either version 2
      of the License, or (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place - Suite 330,
      Boston, MA  02111-1307, USA.

      *****************************************************************

      Program:      ubNetwork
      File:         sources/cub_pop_globals.h
      Author:       Andreas Loeffler
      Date:         Mon, 10.04.2001
      Version:      1.0
      Description:  Global defines & definitions for pop class (RFC 1939)

      -----------------------------------------------------------------

      Changes:		-
      Date:			-
      Description:	-

      *****************************************************************/

    #ifndef __CUB_POP_GLOBALS_H__
    #define __CUB_POP_GLOBALS_H__

    //////////////////////////////////////////////////////////////////////////////////////////
    // Header files
    //////////////////////////////////////////////////////////////////////////////////////////
    #include <stdio.h>

    //////////////////////////////////////////////////////////////////////////////////////////
    // Length defines
    //////////////////////////////////////////////////////////////////////////////////////////

    // Global POP max definitions
    #define CUB_POP_MAX_MESSAGE				512
    #define CUB_POP_MAX_COMMAND				510
    #define CUB_POP_MAX_ARGUMENT			40

    #endif

Currently browsing [ub_pop3_10.zip] (55,328 bytes) - [Sources/cub_n_client.cpp] - (6,264 bytes)

    /******************************************************************
      (C) Copyright 2001 by UNITED BYTES

      web:  http://www.unitedbytes.de
            http://www.unitedbytes.org
    		http://www.unitedbytes.net
      mail: info@unitedbytes.de

      This program is free software; you can redistribute it and/or
      modify it under the terms of the GNU General Public License
      as published by the Free Software Foundation; either version 2
      of the License, or (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place - Suite 330,
      Boston, MA  02111-1307, USA.

      *****************************************************************

      Program:      ubNetwork
      File:         sources/cub_n_client.cpp
      Author:       Andreas Loeffler
      Date:         Mon, 10.04.2001
      Version:      1.0
      Description:  Client base class for internet applications

      -----------------------------------------------------------------

      Changes:		-
      Date:			-
      Description:	-

      *****************************************************************/

    #include "cub_n_client.h"

    ////////////////////////////////////////////
    // States:
    //			1:	WinSock init
    //			2:  Socket created & binded
    //
    ////////////////////////////////////////////

    CUB_Client::CUB_Client ()
    {
    	// Set init level
    	m_iInitLevel = CUB_NS_NOT_INITIALIZED;

    	// Clear error text
    	memset (m_cErrorText, 0, sizeof(m_cErrorText));
    }

    CUB_Client::~CUB_Client ()
    {
    	Shutdown ();
    }

    bool CUB_Client::Setup (char *cServerIP, int iPort)
    {
    	if ((iPort == 0) ||
    		(!strcmp (cServerIP  , "")))
    	{
    		sprintf (m_cErrorText, "CUB_Client->Setup: Wrong connect parameters given! Please check them!");
    		return false;
    	}
    	else {
    		sprintf (m_cServerIP, "%s", cServerIP);
    		m_iServerPort = iPort;
    	}

    	return true;
    }

    bool CUB_Client::Initialize ()
    {
    	WSADATA WSAData;
    	int iError = 0;

    	if (m_iInitLevel > CUB_NS_NOT_INITIALIZED)
    	{
    		sprintf (m_cErrorText, "CUB_Client->Initialize: Client already initialized!");
    		iError = 1;
    	}
    	else {
    		// Call "WSAStartup", request WSA version 2.2 or 1.1
    		iError = WSAStartup(MAKEWORD(2, 2), &WSAData);

    		if (iError != 0) {
    			sprintf (m_cErrorText, "CUB_Client->Initialize: Could not find a valid WinSock DLL!");
    			iError = 1;
    		}
    		else {
    			m_iInitLevel = CUB_NS_WINSOCK_INIT;

    			if (!CreateSocket()) {
    				iError = 1;
    			}
    			else {
    				sprintf (m_cErrorText, "CUB_Client->Initialize: Success.");
    			}
    		}
    	}

    	if (iError == 1)
    		return false;

    	return true;
    }

    bool CUB_Client::Shutdown ()
    {
    	int iError = 0;

    	if (m_iInitLevel < CUB_NS_WINSOCK_INIT)
    	{
    		iError = 1;
    		sprintf (m_cErrorText, "CUB_Client->Shutdown: Can´t shutdown, cause server is not initialized yet!");
    	}
    	else {
    		switch (m_iInitLevel)
    		{
    			case CUB_NS_CONNECTED:

    			case CUB_NS_SOCKET_CREATED:
    				m_Socket.CloseSocket ();

    			case CUB_NS_WINSOCK_INIT:
    				WSACleanup();
    				m_iInitLevel = CUB_NS_NOT_INITIALIZED;
    				break;

    			default:
    				sprintf (m_cErrorText, "CUB_Client->Shutdown: Wrong InitLevel detected!");
    				iError = 1;
    				break;
    		}
    	}

    	memset (m_cServerIP, 0, sizeof(m_cServerIP));
    	m_iServerPort = 0;

    	if (iError == 1)
    		return false;

    	sprintf (m_cErrorText, "CUB_Client->Shutdown: Success.");
    	return true;
    }

    bool CUB_Client::CloseConnection ()
    {
    	int iError = 0;

    	if (m_iInitLevel != CUB_NS_CONNECTED)
    	{
    		sprintf (m_cErrorText, "CUB_Client->CloseConnection: Not connected yet!");
    		iError = 1;
    	}
    	else {

    		if (!m_Socket.CloseSocket ()) {
    			sprintf (m_cErrorText, "CUB_Client->CloseConnection: Can´t close socket!");
    			iError = 1;
    		}
    		else {
    			m_iInitLevel = CUB_NS_SOCKET_CREATED;
    		}
    	}

    	if (iError == 1)
    		return false;

    	sprintf (m_cErrorText, "CUB_Client->CloseConnection: Success.");
    	return true;
    }

    bool CUB_Client::Connect ()
    {
    	int iError = 0;

    	if (m_iInitLevel != CUB_NS_SOCKET_CREATED)
    	{
    		sprintf (m_cErrorText, "CUB_Client->Connect: Wrong InitLevel detected!");
    		iError = 1;
    	}
    	else {
    		if (!m_Socket.Connect (m_cServerIP, m_iServerPort)) {
    			sprintf (m_cErrorText, "CUB_Client->Connect: Connection attempt to address %s, port %d failed!", m_cServerIP, m_iServerPort);
    			iError = 1;
    		}
    		else {
    			// Client successfully connected
    			m_iInitLevel = CUB_NS_CONNECTED;
    		}
    	}

    	if (iError == 1)
    		return false;

    	sprintf (m_cErrorText, "CUB_Client->Connect: Success. (Host=%s, Port=%d)", m_cServerIP, m_iServerPort);
    	return true;
    }

    bool CUB_Client::CreateSocket ()
    {
    	int iError = 0;

    	if (m_iInitLevel != CUB_NS_WINSOCK_INIT)
    	{
    		sprintf (m_cErrorText, "CUB_Client->CreateSocket: Wrong InitLevel detected!");
    		iError = 1;
    	}
    	else {
    		// Create the socket
    		if (!m_Socket.CreateSocket()) {
    			sprintf (m_cErrorText, "CUB_Client->CreateSocket: Cannot create socket! WinSock-Error: %d", GetLastError());
    			iError = 1;
    		}
    		else {
    			// Set socket to non blocking mode
    			// m_Socket.SetSocketBlockingMode (false);

    			// Set the init level
    			m_iInitLevel = CUB_NS_SOCKET_CREATED;
    		}
    	}

    	if (iError == 1)
    		return false;

    	sprintf (m_cErrorText, "CUB_Client->CreateSocket: Success.");
    	return true;
    }

    bool CUB_Client::GetLocalIP (char *cBuffer)
    {
    	int iError = 0;

    	if (m_iInitLevel < CUB_NS_WINSOCK_INIT)
    	{
    		sprintf (m_cErrorText, "CUB_Client->GetLocalIP: Wrong InitLevel detected!");
    		iError = 1;
    	}
    	else {
    		if (!m_Socket.GetLocalIP (cBuffer)) {
    			sprintf (m_cErrorText, "CUB_Client->GetLocalIP: Could not lookup own IP address!");
    			iError = 1;
    		}
    	}

    	if (iError == 1)
    		return false;

    	sprintf (m_cErrorText, "CUB_Client->GetLocalIP: (%s) Success.", cBuffer);
    	return true;
    }

    bool CUB_Client::IsConnected ()
    {
    	if (m_iInitLevel >= CUB_NS_CONNECTED)
    		return true;

    	return false;
    }

Currently browsing [ub_pop3_10.zip] (55,328 bytes) - [Sources/cub_n_socket.h] - (2,700 bytes)

    /******************************************************************
      (C) Copyright 2001 by UNITED BYTES

      web:  http://www.unitedbytes.de
            http://www.unitedbytes.org
    		http://www.unitedbytes.net
      mail: info@unitedbytes.de

      This program is free software; you can redistribute it and/or
      modify it under the terms of the GNU General Public License
      as published by the Free Software Foundation; either version 2
      of the License, or (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place - Suite 330,
      Boston, MA  02111-1307, USA.

      *****************************************************************

      Program:      ubNetwork
      File:         sources/cub_n_socket.h
      Author:       Tim C. Schroeder, modified by Andreas Loeffler
      Date:         Mon, 10.04.2001
      Version:      1.0
      Description:  Simple socket class

      -----------------------------------------------------------------

      Changes:		-
      Date:			-
      Description:	-

      *****************************************************************/

    #ifndef __CUB_NETWORK_SOCKET_H__
    #define __CUB_NETWORK_SOCKET_H__

    #include <winsock.h>

    class CUB_Socket
    {
    	public:
    						CUB_Socket();
    						CUB_Socket(SOCKET iSocket);
    		virtual		   ~CUB_Socket();

    		bool			Bind		 (unsigned int iPort, bool bListen);
    		int				Select		 (bool bRead, bool bWrite, int iTimeOut);
    		bool			Connect		 (char szIPAddress[], unsigned int iPort);
    		void			GetLastError (char szErrDescription[]);
    		bool			GetRemoteIP  (char szIPAddress[]);
    		bool		    GetLocalIP   (char szIPAddress[]);
    		int				Send		 (char *cBuffer, long lBufLen);
    		int				Receive		 (char *cBuffer, long lBufLen);
    		bool			ResolveHost  (char *cIP, char *cResponseBuffer);
    		bool			CanAccept	 ();
    		bool			CanReceive   ();
    		CUB_Socket	   *Accept		 (struct sockaddr *addr,  int *addrlen);
    		bool			CloseSocket  ();
    		bool			Peek         ();
    		bool			ClearSocketBuffer ();
    		bool			Wait         (long lNetworkEvents, DWORD dwTimeOut);
    		void			ResolveHost  (char szHost[]);
    		bool			SetSocketBlockingMode (bool bBlocking);

    		SOCKET			m_sock;
    		struct			sockaddr_in m_sa;
    		bool			CreateSocket();

    		bool			Init();
    		static int		m_iInstCout;	 // Use this to prevent multiple initialisations of the WSA
    };

    #endif

Currently browsing [ub_pop3_10.zip] (55,328 bytes) - [Sources/cub_n_socket.cpp] - (18,103 bytes)

    /******************************************************************
      (C) Copyright 2001 by UNITED BYTES

      web:  http://www.unitedbytes.de
            http://www.unitedbytes.org
    		http://www.unitedbytes.net
      mail: info@unitedbytes.de

      This program is free software; you can redistribute it and/or
      modify it under the terms of the GNU General Public License
      as published by the Free Software Foundation; either version 2
      of the License, or (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place - Suite 330,
      Boston, MA  02111-1307, USA.

      *****************************************************************

      Program:      ubNetwork
      File:         sources/cub_n_socket.cpp
      Author:       Tim C. Schroeder, modified by Andreas Loeffler
      Date:         Mon, 10.04.2001
      Version:      1.0
      Description:  Simple socket class

      -----------------------------------------------------------------

      Changes:		-
      Date:			-
      Description:	-

      *****************************************************************/

    #include "cub_n_socket.h"

    // Static member variables
    int CUB_Socket::m_iInstCout = 0;

    //////////////////////////////////////////////////////////////////////
    // Construcktion / destruction
    //////////////////////////////////////////////////////////////////////

    CUB_Socket::CUB_Socket()
    {
    	Init();
    }

    CUB_Socket::CUB_Socket(SOCKET iSocket)
    {
    	Init();
    	m_sock = iSocket;
    }

    bool CUB_Socket::Init()
    {
    	//////////////////////////////////////////////////////////////////////
    	// Init member variables
    	//////////////////////////////////////////////////////////////////////

    	m_sock = 0;

    	m_iInstCout++;

    	if (m_iInstCout == 1)
    	{
    	}

    	return true;
    }

    CUB_Socket::~CUB_Socket()
    {
    	m_iInstCout--;

    	// Close the socket (if opened)
    	CloseSocket();
    }

    bool CUB_Socket::CreateSocket()
    {
    	//////////////////////////////////////////////////////////////////////
    	// Create the socket
    	//////////////////////////////////////////////////////////////////////

    	if (!m_sock)
    	{
    		m_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
    		if (m_sock == SOCKET_ERROR)
    			return false;
    		return true;
    	}

    	return false;
    }

    bool CUB_Socket::CloseSocket()
    {
    	//////////////////////////////////////////////////////////////////////
    	// Close the socket
    	//////////////////////////////////////////////////////////////////////

    	if (m_sock)
    	{
    		closesocket(m_sock);
    		m_sock = 0;
    		return true;
    	}

    	return true;
    }

    bool CUB_Socket::Connect(char szIPAddress[], unsigned int iPort)
    {
    	char cBuffer [1024];

    	//////////////////////////////////////////////////////////////////////
    	// Connect to the given host
    	//////////////////////////////////////////////////////////////////////

    	// Don't connect if no IP is present
    	if (szIPAddress[0] == '\0')
    		return false;

    	if (iPort <= 0)
    		return false;

    	//////////////////////////////////////////////////////////////////////
    	// Open the connection on the socket
    	//////////////////////////////////////////////////////////////////////

    	// Get the IP
    	ResolveHost (szIPAddress, cBuffer);

    	m_sa.sin_family           = AF_INET;
    	m_sa.sin_port             = htons(iPort);
    	m_sa.sin_addr.S_un.S_addr = inet_addr(cBuffer);

    	// Call "connect" with passed IP address and the passed port
    	if (connect(m_sock, (SOCKADDR *) &m_sa, sizeof (m_sa)) == SOCKET_ERROR)
    		return false;

    	return true;
    }

    bool CUB_Socket::ResolveHost (char *cIP, char *cResponseBuffer)
    {
    	int		iError = 0;
    	struct	in_addr  sInAddr;
    	struct  hostent *spHe = gethostbyname (cIP);

    	// No IP returned ?
    	if (!spHe)
    	{
    		if (cResponseBuffer != NULL) {
    			strcpy(cResponseBuffer, "CUB_Server->ResolveHost: Host is unknown!");
    			iError = 1;
    		}
    	}
    	else {
    		// Extract the IP and replace the host name with it
    		memcpy (&sInAddr, spHe->h_addr_list[0], sizeof(struct in_addr));

    		if (cResponseBuffer != NULL)
    			strcpy (cResponseBuffer, inet_ntoa(sInAddr));
    	}

    	if (iError == 1)
    		return false;

    	return true;
    }

    bool CUB_Socket::Bind (unsigned int iPort, bool bListen)
    {
    	//////////////////////////////////////////////////////////////////////
    	// Bind the socket to the given port
    	//////////////////////////////////////////////////////////////////////

    	// Disconnect and close if a connection / socket is alive
    	if (m_sock != 0)
    		CloseSocket();

    	// Create a new socket
    	if (!CreateSocket())
    		return false;

    	m_sa.sin_addr.s_addr = htonl(INADDR_ANY);
    	m_sa.sin_family = AF_INET;
    	m_sa.sin_port = htons(iPort);

    	// Bind
    	if (bind(m_sock, (struct sockaddr *) &m_sa, sizeof(m_sa)) == SOCKET_ERROR)
    		return false;

    	if (bListen)
    	{
    		// Set backlog
    		if (listen(m_sock, 5) != 0)
    			return false;
    	}

    	return true;
    }

    bool CUB_Socket::CanAccept()
    {
    	//////////////////////////////////////////////////////////////////////
    	// Returns true if there is at least one incomming connection request
    	//////////////////////////////////////////////////////////////////////

    	struct timeval sTimeVal;
    	fd_set		   sFd;
    	int			   iRv;

    	FD_ZERO (&sFd);
    	FD_SET  (m_sock, &sFd);

    	sTimeVal.tv_sec = 0;
    	sTimeVal.tv_usec = 1;

    	iRv = select (1, &sFd, NULL, NULL, &sTimeVal);

    	return iRv ? true : false;
    }

    CUB_Socket *CUB_Socket::Accept(sockaddr *addr, int *addrlen)
    {
    	//////////////////////////////////////////////////////////////////////
    	// Accepts incoming connections on the socket and returns the socket
    	//////////////////////////////////////////////////////////////////////

    	SOCKET ClientSocket;
    	CUB_Socket *pSocket = 0;

    	if (!m_sock)
    		return 0;

    	// Accept connection
    	ClientSocket = accept(m_sock, addr, addrlen);

    	// Connection established ?
    	if (ClientSocket == INVALID_SOCKET)
    		return 0;

    	// Create new socket class to connect to the client
    	pSocket = new CUB_Socket(ClientSocket);

    	return pSocket;
    }

    bool CUB_Socket::ClearSocketBuffer ()
    {
    	//////////////////////////////////////////////////////////////////////
    	// Clears the complete socket buffer
    	//////////////////////////////////////////////////////////////////////

    	int  iReturn;
    	char cBuffer[255];

    	if (!m_sock)
    		return false;

    	// If data old data in socket, clear it!
    	while (Peek()) {
    		iReturn = recv(m_sock, cBuffer, sizeof(cBuffer), 0);

    		if (iReturn == 0 || iReturn == SOCKET_ERROR)
    			break;
    	}

    	return true;
    }

    int CUB_Socket::Receive (char *cBuffer, long lBufLen)
    {
    	//////////////////////////////////////////////////////////////////////
    	// Receive data from a connected socket
    	//////////////////////////////////////////////////////////////////////

    	int  iReturn;

    	if (!m_sock)
    		return false;

    	if (!cBuffer)
    		return false;

    	// Clear the buffer
    	memset (cBuffer, 0, sizeof(lBufLen));

    	// Receive the data
    	iReturn = recv(m_sock, cBuffer, lBufLen, 0);

    	return iReturn;
    }

    bool CUB_Socket::Peek()
    {
    	//////////////////////////////////////////////////////////////////////
    	// Return true if data is available on the connected socket
    	//////////////////////////////////////////////////////////////////////

    	int iReturn;
    	char szBuffer[1];

    	if (!m_sock)
    		return 0;

    	SetSocketBlockingMode (false);

    	// Receive the data
    	iReturn = recv(m_sock, szBuffer, sizeof(szBuffer), MSG_PEEK);

    	SetSocketBlockingMode (true);

    	// Error ?
    	if (iReturn == 0 || iReturn == SOCKET_ERROR)
    		return false;

    	return true;
    }

    bool CUB_Socket::Wait(long lNetworkEvents, DWORD dwTimeOut)
    {
    /*	//////////////////////////////////////////////////////////////////////
    	// Wait for one of of the lNetworkEvents to become signaled
    	//////////////////////////////////////////////////////////////////////

    	WSAEVENT hEvent;
    	DWORD dwReturn;

    	if (!m_sock)
    		return false;

    	// Create the event object
    	hEvent = WSACreateEvent();

    	// Let hEvent be triggered by lNetworkEvents
    	if (WSAEventSelect(m_sock, hEvent, lNetworkEvents) == SOCKET_ERROR)
    		return false;

    	// Wait for the event(s)
    	dwReturn = WSAWaitForMultipleEvents(1, &hEvent, FALSE, dwTimeOut, FALSE);

    	// Close the event
    	WSACloseEvent(hEvent);

    	// Evaluate the return value
    	if (dwReturn == WSA_WAIT_TIMEOUT ||
    		dwReturn == WSA_WAIT_FAILED)
    	{
    		// Wait has failed
    		return false;
    	}*/

    	// Event triggered
    	return true;
    }

    int CUB_Socket::Send (char *cBuffer, long lBufLen)
    {
    	//////////////////////////////////////////////////////////////////////
    	// Send data over a connected socket
    	//////////////////////////////////////////////////////////////////////

    	int iReturn = 0;

    	if (!m_sock)
    		return 0;

    	// Send the data
    	iReturn = send (m_sock, cBuffer, lBufLen, 0);

    	// Error ?
    	if (iReturn == SOCKET_ERROR)
    		return false;

    	return iReturn;
    }

    bool CUB_Socket::GetLocalIP(char szIPAddress[])
    {
    	//////////////////////////////////////////////////////////////////////
    	// Return the IP adress of the last local interface. This is most
    	// likely the PPP adapter (if connected)
    	//////////////////////////////////////////////////////////////////////

    	char ac[80];
    	struct in_addr addr;
    	unsigned int i;

    	// Get the local host name
    	if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR)
    		return false;

    	struct hostent *phe = gethostbyname(ac);

    	// Test up to 64 addresses
    	for (i=0; i<64; i++)
    	{
    		// Last IP found ?
    		if (phe->h_addr_list[i+1] == 0)
    		{
    			// Return
    			memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));
    			strcpy(szIPAddress, inet_ntoa(addr));
    			break;
    		}
        }

    	return true;
    }

    bool CUB_Socket::GetRemoteIP(char szIPAddress[])
    {
    	//////////////////////////////////////////////////////////////////////
    	// Return the IP adress of the peer
    	//////////////////////////////////////////////////////////////////////

    	struct sockaddr_in name;
    	int iLen;

    	if (!m_sock)
    		return false;

    	// Size of the sockaddr_in structure
    	iLen = sizeof(sockaddr_in);

    	// Retrieve the peer name
    	if (getpeername(m_sock, (sockaddr *) &name, &iLen) == SOCKET_ERROR)
    		return false;

    	// Copy the string into the passed buffer
    	strcpy(szIPAddress, inet_ntoa(name.sin_addr));

    	return true;
    }

    void CUB_Socket::ResolveHost(char szHost[])
    {
    	//////////////////////////////////////////////////////////////////////
    	// Resolve the passed host to an IP address
    	//////////////////////////////////////////////////////////////////////

    	struct in_addr addr;
    	struct hostent *phe = gethostbyname(szHost);

    	// No IP returned ?
    	if (!phe)
    	{
    		strcpy(szHost, "<Unknown>");
    		return;
    	}

    	// Extract the IP and replace the host name with it
    	memcpy(&addr, phe->h_addr_list[0], sizeof(struct in_addr));
    	strcpy(szHost, inet_ntoa(addr));
    }

    int CUB_Socket::Select(bool bRead, bool bWrite, int iTimeOut)
    {
    	//////////////////////////////////////////////////////////////////////
    	// Determine the status of the connected socket
    	//////////////////////////////////////////////////////////////////////

    	fd_set ReadSocket;
    	fd_set WriteSocket;
    	timeval TimeOut;
    	int iReturn;

    	// Set timeout
    	TimeOut.tv_sec = iTimeOut;			// Old: / 1000;
    	TimeOut.tv_usec = 0;

    	// Fill socket sets
    	ReadSocket.fd_array[0] = m_sock;
    	ReadSocket.fd_count = 1;
    	WriteSocket.fd_array[0] = m_sock;
    	WriteSocket.fd_count = 1;

    	// Make the requested select call
    	iReturn = select(0, bRead ? &ReadSocket : 0, bWrite ? &WriteSocket : 0,
    		0, &TimeOut);

    	if (iReturn == SOCKET_ERROR)
    		return 0;

    	return iReturn;
    }

    bool CUB_Socket::CanReceive ()
    {
    	//////////////////////////////////////////////////////////////////////
    	// Determine the status of the connected socket
    	//////////////////////////////////////////////////////////////////////

    	fd_set  lReceiveSocket;
    	timeval TimeOut;

    	lReceiveSocket.fd_count    = 1;
    	lReceiveSocket.fd_array[0] = m_sock;

    	TimeOut.tv_sec = 0;
    	TimeOut.tv_usec = 0;

    	// Determine status of the socket
    	if (select (0, &lReceiveSocket, NULL, NULL, &TimeOut) == 1)
    		return true;
    	else
    		return false;
    }

    bool CUB_Socket::SetSocketBlockingMode (bool bBlocking)
    {
    	//////////////////////////////////////////////////////////////////////
    	// Turns the blocking mode on/off
    	//////////////////////////////////////////////////////////////////////

    	unsigned long lMode = 0;

    	if (bBlocking)
    		lMode = 0;
    	else
    		lMode = 1;

        if (ioctlsocket (m_sock, FIONBIO, &lMode) == SOCKET_ERROR)
            return false;

    	return true;
    }

    void CUB_Socket::GetLastError(char *szErrDescription)
    {
    	//////////////////////////////////////////////////////////////////////
    	// Return a string for the last error
    	//////////////////////////////////////////////////////////////////////

    	// Retrieve the last error
    	int iError = WSAGetLastError();

    	switch (iError)
    	{
    		case 0:
    			strcpy(szErrDescription, "No error");
    			break;
    		case WSAEINTR:
    			strcpy(szErrDescription, "Interrupted system call");
    			break;
    		case WSAEBADF:
    			strcpy(szErrDescription, "Bad file number");
    			break;
    		case WSAEACCES:
    			strcpy(szErrDescription, "Permission denied");
    			break;
    		case WSAEFAULT:
    			strcpy(szErrDescription, "Bad address");
    			break;
    		case WSAEINVAL:
    			strcpy(szErrDescription, "Invalid argument");
    			break;
    		case WSAEMFILE:
    			strcpy(szErrDescription, "Too many open sockets");
    			break;
    		case WSAEWOULDBLOCK:
    			strcpy(szErrDescription, "Operation would block");
    			break;
    		case WSAEINPROGRESS:
    			strcpy(szErrDescription, "Operation now in progress");
    			break;
    		case WSAEALREADY:
    			strcpy(szErrDescription, "Operation already in progress");
    			break;
    		case WSAENOTSOCK:
    			strcpy(szErrDescription, "Socket operation on non-socket");
    			break;
    		case WSAEDESTADDRREQ:
    			strcpy(szErrDescription, "Destination address required");
    			break;
    		case WSAEMSGSIZE:
    			strcpy(szErrDescription, "Message too long");
    			break;
    		case WSAEPROTOTYPE:
    			strcpy(szErrDescription, "Protocol wrong type for socket");
    			break;
    		case WSAENOPROTOOPT:
    			strcpy(szErrDescription, "Bad protocol option");
    			break;
    		case WSAEPROTONOSUPPORT:
    			strcpy(szErrDescription, "Protocol not supported");
    			break;
    		case WSAESOCKTNOSUPPORT:
    			strcpy(szErrDescription, "Socket type not supported");
    			break;
    		case WSAEOPNOTSUPP:
    			strcpy(szErrDescription, "Operation not supported on socket");
    			break;
    		case WSAEPFNOSUPPORT:
    			strcpy(szErrDescription, "Protocol family not supported");
    			break;
    		case WSAEAFNOSUPPORT:
    			strcpy(szErrDescription, "Address family not supported");
    			break;
    		case WSAEADDRINUSE:
    			strcpy(szErrDescription, "Address already in use");
    			break;
    		case WSAEADDRNOTAVAIL:
    			strcpy(szErrDescription, "Can't assign requested address");
    			break;
    		case WSAENETDOWN:
    			strcpy(szErrDescription, "Network is down");
    			break;
    		case WSAENETUNREACH:
    			strcpy(szErrDescription, "Network is unreachable");
    			break;
    		case WSAENETRESET:
    			strcpy(szErrDescription, "Net connection reset");
    			break;
    		case WSAECONNABORTED:
    			strcpy(szErrDescription, "Software caused connection abort");
    			break;
    		case WSAECONNRESET:
    			strcpy(szErrDescription, "Connection reset by peer");
    			break;
    		case WSAENOBUFS:
    			strcpy(szErrDescription, "No buffer space available");
    			break;
    		case WSAEISCONN:
    			strcpy(szErrDescription, "Socket is already connected");
    			break;
    		case WSAENOTCONN:
    			strcpy(szErrDescription, "Socket is not connected");
    			break;
    		case WSAESHUTDOWN:
    			strcpy(szErrDescription, "Can't send after socket shutdown");
    			break;
    		case WSAETOOMANYREFS:
    			strcpy(szErrDescription, "Too many references, can't splice");
    			break;
    		case WSAETIMEDOUT:
    			strcpy(szErrDescription, "Connection timed out");
    			break;
    		case WSAECONNREFUSED:
    			strcpy(szErrDescription, "Connection refused");
    			break;
    		case WSAELOOP:
    			strcpy(szErrDescription, "Too many levels of symbolic links");
    			break;
    		case WSAENAMETOOLONG:
    			strcpy(szErrDescription, "File name too long");
    			break;
    		case WSAEHOSTDOWN:
    			strcpy(szErrDescription, "Host is down");
    			break;
    		case WSAEHOSTUNREACH:
    			strcpy(szErrDescription, "No route to host");
    			break;
    		case WSAENOTEMPTY:
    			strcpy(szErrDescription, "Directory not empty");
    			break;
    		case WSAEPROCLIM:
    			strcpy(szErrDescription, "Too many processes");
    			break;
    		case WSAEUSERS:
    			strcpy(szErrDescription, "Too many users");
    			break;
    		case WSAEDQUOT:
    			strcpy(szErrDescription, "Disc quota exceeded");
    			break;
    		case WSAESTALE:
    			strcpy(szErrDescription, "Stale NFS file handle");
    			break;
    		case WSAEREMOTE:
    			strcpy(szErrDescription, "Too many levels of remote in path");
    			break;
    		case WSASYSNOTREADY:
    			strcpy(szErrDescription, "Network system is unavailable");
    			break;
    		case WSAVERNOTSUPPORTED:
    			strcpy(szErrDescription, "Winsock version out of range");
    			break;
    		case WSANOTINITIALISED:
    			strcpy(szErrDescription, "WSAStartup not yet called");
    			break;
    		case WSAEDISCON:
    			strcpy(szErrDescription, "Graceful shutdown in progress");
    			break;
    		default:
    			strcpy(szErrDescription, "Unknown error");
    			break;
    	}
    }

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.