Click here to Skip to main content
15,892,809 members
Articles / Desktop Programming / MFC

Single Server With Multiple Clients: A Simple C++ Implementation

Rate me:
Please Sign up or sign in to vote.
4.80/5 (100 votes)
22 Jul 200414 min read 1.6M   17.4K   196  
Implement a client/server structure with multiple clients using simple C++ classes
/* 

  Liyang Yu, Jan 9th, 2004, version 0.0

  this is to implement the domain and IP address resolution. 

  3 cases are considered:

  1. a host name is given (a host name looks like "www.delta.com"), query
     the IP address of the host

  2. an IP address is given (an IP address looks like 10.6.17.184), query
     the host name

  in the above two cases, the IP address and the host name are the same thing: 
  since IP address is hard to remember, they are usually aliased by a name, and this 
  name is known as the host name.

  3. nothing is given. in other words, we don't know the host name or the IP address.
     in this case, the standard host name for the current processor is used
     
*/

#ifndef myHostInfo_H
#define myHostInfo_H

#include <string>
using namespace std;

// this version is for both Windows and UNIX, the following line
// specifies that this is for WINDOWS
#ifndef WINDOWS_XP
	#define WINDOWS_XP
#endif

// #include <XPCException.h>  // add this later

#ifdef UNIX
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <netinet/in.h>
    #include <sys/socket.h>
#else
    #include <winsock2.h>
#endif
#include <stdio.h>
    
enum hostType {NAME, ADDRESS};
const int HOST_NAME_LENGTH = 64;

class myHostInfo
{

private:

	#ifdef UNIX
		char searchHostDB;     // search the host database flag
	#endif

	struct hostent *hostPtr;    // Entry within the host address database

public:

    // Default constructor
    myHostInfo();

    // Retrieves the host entry based on the host name or address
    myHostInfo(string& hostName, hostType type);
 
    // Destructor.  Closes the host entry database.
    ~myHostInfo()
    {
		#ifdef UNIX
			endhostent();
		#endif
    }

	#ifdef UNIX

		// Retrieves the next host entry in the database
		char getNextHost();

		// Opens the host entry database
		void openHostDb()
		{
			endhostent();
			searchHostDB = 1;
			sethostent(1);
		}

	#endif

    // Retrieves the hosts IP address
    char* getHostIPAddress() 
    {
        struct in_addr *addr_ptr;
		// the first address in the list of host addresses
        addr_ptr = (struct in_addr *)*hostPtr->h_addr_list;
		// changed the address format to the Internet address in standard dot notation
        return inet_ntoa(*addr_ptr);
    }    
    
    // Retrieves the hosts name
    char* getHostName()
    {
        return hostPtr->h_name;
    }

private:

	#ifdef WINDOWS_XP
		void detectErrorGethostbyname(int*,string&);
		void detectErrorGethostbyaddr(int*,string&);
	#endif
};

#endif

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.


Written By
Web Developer
United States United States
I love to tell jokes and today, I finally came up with my own joke. here it goes:

I decide to make my first son a medical doctor so later on when I am old and sick, I can get medical care any time I need and for free..., in fact, better to make my second son a medical doctor too so I can get a second opinion.

here is another version of this joke:

I decide to make my first son a medical doctor so later on when I am old and sick, I can get medical care any time I need and for free..., in fact, better to make my second son a medical doctor too so I can get a second opinion. well, perhaps my third son should be a lawyer - in case something is wrong with my medical care, I can sue the first two for free.

if you happen to visit this page and read these two jokes, tell me which one you like...

Comments and Discussions