Click here to Skip to main content
13,260,383 members (43,590 online)
Click here to Skip to main content
Add your own
alternative version


45 bookmarked
Posted 19 Dec 2004

Network Administrator

, 19 Dec 2004
Rate this:
Please Sign up or sign in to vote.
The article shows how you can work with a multithreading client server application and administrate computers on a network.


The program I will present is a simple application that will point out certain little things that usually you cannot handle while programming a client server application. The application client waits for connection from the computers that are on the network and which are configured to connect to that certain computer as I will show later on. The tricky problems that I've achieved with this program were making the multithreading server and client work in parallel, and I think that I've also achieved some synchronization of the two. There is also the fact that it helps you with many issues regarding your computer (like hiding the desktop or the taskbar or Start button).


The main ideas that I need to present about my article include, first of all, the IP retrieving from the connected computer, the threads work in parallel to synchronize the packet sending, and finally the miscellaneous like retrieving processes, hiding tray...

Using the code

First, I will start by presenting the Server whose task is to try to connect to a certain local network IP that you configure it on. All we have to do is make some basic initialization:

hInst=hInstance; //get handle of the current instance to use it later
hicon=(HICON)LoadImage(. . . .);

Now, after we have our icon loaded and we have the handle to the instance in the hInst variable declared as a global, we have to check if the application is running for the first time. The way to do this is to check for a certain key in the registry where we will store later the info about the remote host. If the key and value exists then the application is not running for the first time, otherwise it runs for the first time and we have to make a default host set to

HKEY Regentry;
DWORD dispos,dwSize,dwType;
char primul[1024];

RegCreateKeyEx(HKEY_LOCAL_MACHINE,"Software\\RemoteHost" . .. . . );

RegQueryValueEx(Regentry, "host", NULL, &dwType, 
               (unsigned char*)&primul, &dwSize);
if(RegQueryValueEx(Regentry,"host", NULL, &dwType, 
                  (unsigned char*)&primul, &dwSize)!=ERROR_SUCCESS)
//the aplication runs for the first time 
  //initialize the host that will be used later from 
  //the reg when enumerating the computers on the network
  char host[100];
             (unsigned char*)&host,strlen(host)+1);

Now, we have to start our first thread which will monitor if a certain key combination from the keyboard is pressed. If that certain key combination is pressed then a dialog will appear and ask you to enter the host of the computer on the network which has the client.

      goto begin;
  return 0;

Importantly, remember that this is a thread that will monitor if these keys are pressed, and if they are, a dialog will come up and you will enter the host in an edit box. After pressing OK, the host will be saved in the registry as the default host to connect to.

case IDOK:
  char host[1024];
  if (strcmp(host,"")==0)
    MessageBox(NULL,"Error no host inserted","",MB_OK);
  HKEY Regentry;
  DWORD dispos;

  RegCreateKeyEx(HKEY_LOCAL_MACHINE,"Software\\RemoteHost",. . . .. . );
  if (Regentry==NULL)
    MessageBox(NULL,"Error setting host","",MB_OK);
               (unsigned char*)&host,strlen(host)+1);
  MessageBox(NULL,"Host succesfully saved","",MB_OK);

Now, let's go on with the code from the WinMain. After starting this thread, we have to enumerate all computers on the network, and if a certain computer matches the name stored in the registry, then get it's IP and try to connect, otherwise sleep a few milliseconds and try again.

  //we have to set a tag from where 
  //we have to start the process of reconnection if 
  //the computer was not found or just failed

  //I wil explain later this procedure 
  //which enumerates the computer on the lo
  //cal network and if any mathces the one get the IP
  // in the as variable which is a char *


  WSADATA wsaData;
  struct hostent *hp;
  unsigned int addr;
  struct sockaddr_in server;
  int wsaret=WSAStartup(0x101,&wsaData);

    return 0;

  // I said the ip is stored in the as variable, 
  //if no computer was found the ip will be or localhost

  //if this is going to be repetitive cycle 
  //we have to give it some time to sleep

  //init the socket

    goto aici;
  //if any errors we do not return because the server 
  //will stop but we go to the "again" tag 


    goto aici;

  server.sin_addr.s_addr=*((unsigned long*)hp->h_addr);

  if(connect(conn,(struct sockaddr*)&server,sizeof(server)))
    goto again;
    //if we cannot connect the we also go to again

  //if we have a succesful connection the we start 
  //a thread for the client and give it as
  //parameter the connected socket.


  //and ofcourse we need an infinite cycle 
  //for the aplication not to close after we connect
  while (1)
  return 0;

Now, I have to tell you and present the GetIps() void. The procedure just uses the WNetOpenEnum and WNetEnumResource functions to enumerate.

  CString strTemp;
  struct hostent *host;
  struct in_addr *ptr; // To retrieve the IP Address 

  NETRESOURCE *NetResource = NULL;
  HANDLE hEnum;

  The WNetOpenEnum function starts an enumeration 
  of network resources or existing connections. We have to continue 
  the enumeration by calling the WNetEnumResource function.

  WNetOpenEnum( dwScope, NULL, NULL, 
             NULL, &hEnum );
  //but first we have to get ready to also 
  //take the ip of the computer that matches 
  //the host from the registry
  WSADATA wsaData;

  if ( hEnum )
    DWORD Count = 0xFFFFFFFF;
    DWORD BufferSize = 2048;
    LPVOID Buffer = new char[2048];
    //this is the buffer where 
    //the names will come as an array of
    //LPNETRESOURCE variables

    WNetEnumResource( hEnum, &Count, Buffer, &BufferSize );
    NetResource = (NETRESOURCE*)Buffer;

    char szHostName[200];
    unsigned int i;


    HKEY Regentry ;
    DWORD dispos,dwSize,dwType;

    RegCreateKeyEx(HKEY_LOCAL_MACHINE,"Software\\RemoteHost",. . .. );
    if (Regentry==NULL)
      goto papa; //exit the void

    RegQueryValueEx(Regentry,"host" , NULL, 
           &dwType, (unsigned char*)&as, &dwSize);
    RegQueryValueEx(Regentry,"host" , NULL, 
           &dwType, (unsigned char*)&as, &dwSize);

    char *host1;
    host1= _strupr( _strdup( as ) );

    //start the enumeration of the arrays from the buffer
    for ( i = 0; i < BufferSize/sizeof(NETRESOURCE); i++, NetResource++ )
        if ( NetResource->dwUsage ==RESOURCEUSAGE_CONTAINER 
          && NetResource->dwType == RESOURCETYPE_ANY )
            if ( NetResource->lpRemoteName )
              //the name retreived will be given like this \\remote
              //so we will have to look for the \\ caracters and when found
              //copy what's in the right into a string buffer
              CString strFullName = 
              if ( 0 == strFullName.Left(2).Compare("\\\\") )
                strFullName = strFullName.Right(strFullName.GetLength()-2);
              gethostname( szHostName, strlen( szHostName ) );
              host = gethostbyname(strFullName);

              //now the ip retreiving
              //after having the host
              if(host == NULL) continue;
              ptr = (struct in_addr *) host->h_addr_list[0];

              int a = ptr->S_un.S_un_b.s_b1;  //127.
              int b = ptr->S_un.S_un_b.s_b2;  //0.
              int c = ptr->S_un.S_un_b.s_b3;  //0.
              int d = ptr->S_un.S_un_b.s_b4;  //1


              //copy it in the as buffer to use it to connect
    delete Buffer;
    WNetCloseEnum( hEnum ); 
  papa: //out
  return 1;

Now to tell you what the server can do and also explain the client thread. As you've seen, all that the WinMain does is it tries to connect to a certain computer on the network, the computer whose name is in the registry. Now, after we have a successful connection, we have to begin the client thread which will interpret the messages sent from the client. The first thing we should do is send a short message to the client so it will know that the connection was completed successfully.

char buff[1024];
int n;
if ((n==0)||(n==SOCKET_ERROR))

Now after we receive the message from the client, the client will need the IP of the local computer which he is connected to, so we will send the IP. First, we have to get the host, and then turn it into a four dotted char IP address with inet_ntoa().

char text[100];
struct hostent *h; //structure that will hold info on th ip address

gethostname(text,sizeof(text)); //get local host's name
h=gethostbyname(text); //get the info on the host
char * ip;
ip=inet_ntoa(*((struct in_addr *)h->h_addr));
//convert it to an ip address
//and send it

char detr[1024];
strcat(detr," ");

Now, we have to enter a so called infinite cycle that will work the messages received from the client. If any errors occur then we will exit the thread and the client will notice because it won't receive the ping from the server. Very importantly, know that if we want to receive a text from the client, we should always set the memory of the buffer in which we receive 0. So...

while (1)  //the infinite cycle
  ZeroMemory(buff,sizeof(char [1024])); //set it to 0
  if ((n==SOCKET_ERROR)||(n==0))
    reset: //this tag will be very usefull later it resets the server
    char serv[1024];

    return 0;
  /* From time to time the client will send 
  the char ping to the server and waits from
  the pong reply. If it does not receives it 
  or the socket has any errors will 
  consider the server disconnected.

  if (strcmp(buff,"ping")==0)
    Sleep(5); //it is very imporant to know that 
    //before sending any packets of info
    //the computer should sleep a little. I will explain later.

if ((n==SOCKET_ERROR)||(n==0)) goto reset;

This way, all the communication is done. Now the essence of the thread is, giving info on the existing files on the computer, and of course, the synchronization.

So if the client wants to see the files on a certain computer it's connected to, it sends a string that the computer will interpret. The string for the files view is "files". When the client browses the files of the server, it can also make downloads and also delete any files or execute them. The thing is that if he has to do it only from this thread, then he will not be able to deliver any pings or give info on the computer. The things I did were: after the server receives the string "files", it opens another thread called FileClient() that connects to the client again through a totally different socket and port. Of course, the client opens a thread that accepts connection on that port. After it connects, it will create a thread similar to the client thread but it will wait for messages from the fileclient socket. So...

if ((StrStrI(buff,"files")!=NULL))
       (LPVOID)text,0,0); //text being the ip

Now a little bit about synchronization. For example, if the server receives a message from the client that tells it to send the list of processes. After the client sends the message, if it has to do any initialization and the server immediately sends a string, the client might not receive it and that's why I let the server sleep every time it has to send something. It is very important because this happens very often if the connection speed is very good. Now about the file sending process. The server receives a string with a file name. Now the file might also be a directory or might not even exist if the user deletes it after the last refresh of file was made. The thing that we should do is check if the file exists, and then if it is a directory, if it is, send a string that will tell that, otherwise send it.

DWORD attrb;
if ((attrb==FILE_ATTRIBUTE_DIRECTORY)||(attrb==-1)||(attrb==17))
   //17 means that the file is a customized directory 
   //with a different picture and stuff 
   n=send(connfile,"unavalaible",11,0); //send the unavalaible string
   if ((n==SOCKET_ERROR)|| (n==0)) goto reset;

If the file is OK, tell the client that. Now we have to open it and tell the client the file's length, and then send it by reading from the file and sending to the client that will write to the file whatever it gets until the file is all sent.

n=send(connfile,"file ok",8,0);
if ((n==SOCKET_ERROR)|| (n==0)) goto reset;

if ((n==SOCKET_ERROR)|| (n==0)) goto reset;

I did with CFile because it is much easier to get the length and stuff although
I beleive that MFC is less good that WIN32 Apis

CFile f;

DWORD size;
if ((n==SOCKET_ERROR)|| (n==0)) goto reset;

if ((n==SOCKET_ERROR)|| (n==0)) goto reset;

int y;
char buf[2048];
//before starting to send sleep 100 miliseconds 
//so the client will be ready to receive
while (true)
  y=f.Read(buf,1024); //try to read 1024 caracters
  n=send(connfile,buf,y,0);// send the actual numbers of bites read
  if ((n==SOCKET_ERROR)|| (n==0))
    n=send(connfile,"file ready",11,0);
    goto reset;
  //we should also recv something if we want
  //the file to be send succesfully
  if ((n==SOCKET_ERROR)|| (n==0))
    n=send(connfile,"file ready",11,0);
    goto reset;
  if (strcmp(buff,"break")==0)
    goto rename;
  //and we do this cycle untill the bytes read
  //are less than 1024 which means the
  //file is over
  if (y<1024) break;

Now all we have to do is send a string to the client to announce to it that the file is over so it will close the file and stop its cycle. This is all I can tell you about the server, or should I say the main things that it can do? About the client: now, here I don't really have much to say because they are very similar. The tricks that should be mentioned from the client are: sending messages only from certain threads or getting the download speed. The client starts with a thread that waits for connections from any computer on the LAN that has a server started. After one connects, it opens a thread for that certain computer and stores its IP. It is very important to know that after you start a thread, you can only pause it, resume it or kill it, so if I want to send a certain message to a server at a certain time, I have to declare a global variable called command which is a boolean and which each thread checks to see if it has any command to be sent. Take a look:

MSG msg;


  if (command==true) //if there is any command to be sent
  if (StrStrI(comanda,ip)!=NULL)
  // check if the ip of the connected computer is in the command string
    //if it is send it
    if ((n==SOCKET_ERROR)||(n==0)) goto out;

The command boolean will be maneuvered by the user. If it presses the button KillServer for example, the command string will be like this "host+ip killserver", and of course, command=true which means that the computer with that host and IP should terminate its server. The client thread checks if the command boolean is true and if it contains the IP of the connected computer. If yes, send the server message to exit the process. To get the download speed now: I thought that this would be very simple to be done if I compare the length of the file each second and see how much it has grown. I want to know the KB/second speed but this doesn't mean that I should check every second because that would be too much time; you can check every 500 milliseconds and rapport it to 1 second:

500 . . . . . . . . . 124235 KB

1000 . . . . . . . . . X

So if in 500 milliseconds we got 12435 KB, in 1000 milliseconds, we would have got X which equals: X=((1000)*(124235))/500, and this is the number of KB/second the speed of download.

case WM_TIMER:
  if (downloading)
    /* prev and now are the DWORD</CODE>s that contain info on the
       previous and current file length

    DWORD z,rt;
    /* now-prev is the quantity of bytes written 
    in the file in the las t 500 miliseconds
    By dividing it by 1000 we get the number of KB written
    rt=(1000*z)/500; // :)
    char rat[1024];
    strcat(rat," ");
    SetDlgItemText(hdlg,RATE,"0 KB/s" );

That's kind of all. If you have any questions regarding the project, please ask me.


This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


About the Author

Software Developer
Romania Romania
I love VC++

You may also be interested in...


Comments and Discussions

Questionare you know your picture :satan specification Pin
Member 102076231-Oct-14 22:12
memberMember 102076231-Oct-14 22:12 
Questionregarding project Pin
Member 104382872-Dec-13 20:45
memberMember 104382872-Dec-13 20:45 
QuestionHow to make a Remote Shell? [modified] Pin
(BlackBox) Ethical Hacker31-Oct-10 6:01
member(BlackBox) Ethical Hacker31-Oct-10 6:01 
AnswerProgram Pin
(BlackBox) Ethical Hacker31-Oct-10 5:55
member(BlackBox) Ethical Hacker31-Oct-10 5:55 
Generalsoftware Pin
loipham28-Oct-07 16:58
memberloipham28-Oct-07 16:58 
Generalsoftware for running C++ Pin
loipham28-Oct-07 16:52
memberloipham28-Oct-07 16:52 
Generalinternet! Pin
farshad.f5-Jul-06 6:26
memberfarshad.f5-Jul-06 6:26 
GeneralRe: internet! Pin
euacela5-Jul-06 9:59
membereuacela5-Jul-06 9:59 
Generala problem in using ur code! Pin
farshad.f4-Jul-06 22:06
memberfarshad.f4-Jul-06 22:06 
GeneralRe: a problem in using ur code! Pin
euacela5-Jul-06 4:27
membereuacela5-Jul-06 4:27 
GeneralRe: a problem in using ur code! Pin
farshad.f5-Jul-06 5:25
memberfarshad.f5-Jul-06 5:25 
Questionhow to connect between server and client? Pin
Petiao21-Dec-05 15:45
memberPetiao21-Dec-05 15:45 
AnswerRe: how to connect between server and client? Pin
farshad.f5-Jul-06 7:22
memberfarshad.f5-Jul-06 7:22 
GeneralalexmcadaHow To check the CD Writter,Printer, Download Size Status such as How Copies writted and printed Pin
alexmcada3-Oct-05 2:27
memberalexmcada3-Oct-05 2:27 
General&lt;----- Report bugs -----&gt; Pin
bear golden14-May-05 2:35
memberbear golden14-May-05 2:35 
GeneralRe: &lt;----- Report bugs -----&gt; Pin
euacela16-May-05 23:16
membereuacela16-May-05 23:16 
GeneralRe: &lt;----- Report bugs -----&gt; Pin
bear golden17-May-05 18:23
memberbear golden17-May-05 18:23 
GeneralRe: &lt;----- Report bugs -----&gt; Pin
euacela17-May-05 20:58
membereuacela17-May-05 20:58 
GeneralRe: &lt;----- Report bugs -----&gt; Pin
bear golden17-May-05 22:40
memberbear golden17-May-05 22:40 
GeneralRe: &lt;----- Report bugs -----&gt; Pin
Anonymous18-May-05 1:02
sussAnonymous18-May-05 1:02 
GeneralRe: &lt;----- Report bugs -----&gt; Pin
bear golden22-May-05 20:36
memberbear golden22-May-05 20:36 
Generalcompile under VS2003 ERROR Pin
DanverHong1-Feb-05 17:32
memberDanverHong1-Feb-05 17:32 
GeneralRe: compile under VS2003 ERROR Pin
euacela20-Mar-05 0:33
membereuacela20-Mar-05 0:33 
GeneralConnection Problem Pin
MyXOMOP17-Jan-05 5:53
memberMyXOMOP17-Jan-05 5:53 
GeneralRe: Connection Problem Pin
euacela18-Jan-05 10:38
membereuacela18-Jan-05 10:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 20 Dec 2004
Article Copyright 2004 by gamitech
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid