Clients that Find Servers in a Windows Network Domain (TCP/IP, Mailslot)

Introduction

This simple article will demonstrate how a CLIENT application can find its SERVER in a network by using a Windows feature called Mailslot. You can download the demo project and source code at the end of this article.

A Server that BROADCASTs its IP and PORT

Every TCP/IP CLIENT application requires a SERVER IP and PORT to get connected to it. However, when the SERVER IP or/and PORT changes, all CLIENTS must be aware; otherwise, they can not communicate anymore.

How to Make the Server Broadcast Data to a Network

The Windows platform offers a mechanism called Mailslot that is an IPC (Inter-Process Communication) where applications are able to send message-packets (DATAGRAMS) over the network either to a specific computer or to all computers in a specific domain.

A Simple Message Must be Defined

Before showing how Mailslot can be implemented, is worth mentioning that a simple message-packet must be recognized for both sides. The source code contains two projects, msserver and msclient. Both share ms_protocol.h that defines a simple message:

#define MS_MESSAGE_ID 0xaabbccdd

typedef struct _MS_MESSAGE_
{

   DWORD msg_id;       // MS_MESSAGE_ID
   char  ipStr[20];    // Server IP (string format)
   DWORD ip;           // Server IP (numeric format)
   UINT  port;         // Server PORT

} MSMESSAGE;

MSMESSAGE is the message that the SERVER is going to broadcast every second. Notice that there is no answer to send back by the CLIENT. It receives the message and uses information received to start a TCP/IP STREAM connection with SERVER.

Testing the Applications

The project was first coded by using the old VC++ 6 compiler. I have updated it to VS 2005. Both applications are console applications. If you have an older compiler, you won't have a problem in re-creating the projects and building them. The best way to test is on a network having CLIENTs on different machines. But, you can test on a single machine too.

Starting msserver (SERVER), you get a console window that shows IP and PORT in use (see Figure 1).

Figure 1

Immediately after being started, msserver begins to broadcast its position (IP and PORT—see the msserver.cpp, ServerBroadcastThread function). Notice how message-packet sending is done when using mailslots: The CreateFile and WriteFile functions are used.

CreateFile opens, in this case, not a file but a mailslot named "\\\\*\\mailslot\\@_MSClient_@". Notice the naming convention: * means all computers that are reading a mailslot named "\\\\.\\mailslot\\@_MSClient_@". Then, the CLIENT instances over the network open and read mailslots with that name.

If you want, you might use a sniffer to check what the server is broadcasting (see Figure 2).

Figure 2

On starting msclient (CLIENT), you will see the following (see Figure 3):

Figure 3

Each CLIENT instance creates a mailslot namedd "\\\\.\\mailslot\\@_MSClient_@" and starts reading it (in fact, you can have one process using that name each time—see msclient.cpp to learn how to handle that). After it gets the SERVER message-packet, it configures a STREAM socket connection and starts a kind of "PING" with the SERVER.

CreateMailslot is the function used to create a mailslot and ReadFile to read from it. You can have more than one CLIENT per machine. SERVER is a multi-thread application and it can handle many CLIENTs at time.

So, remember the following:

Writing to a Mailslot CreateFile WriteFile
Reading from a Mailslot CreateMailslot ReadFile

Enjoy. Hope this helps.



Downloads

Comments

  • tcp/ip implementation

    Posted by merinm on 05/04/2013 03:36am

    Am planning to implement tcp/ip stack on a xilinx fpga(3E500 series with microblaze processor). Can you help me with a c-code for implementation of tcp/ip.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Managing your company's financials is the backbone of your business and is vital to the long-term health and viability of your company. To continue applying the necessary financial rigor to support rapid growth, the accounting department needs the right tools to most efficiently do their job. Read this white paper to understand the 10 essentials of a complete financial management system and how the right solution can help you keep up with the rapidly changing business world.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds