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: May 18, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT While the idea of using facial and or gesture recognitions to create a modern, intuitive game seems attractive, some developers may want to leverage Unity 3D as a way to accelerate their development. There are many different ways in which Intel and Unity Technologies have been working together to helps speed the develop of games with the Intel® RealSense™ SDK (Software Developer Kit), so come hear from a panel of experts on what we've done …

  • You may not realize the complexity you could be creating when developing mobile apps – many companies don't initially. You could be doubling your IT costs for development and delivery; not to mention, risking sales, productivity and brand satisfaction. Read this IBM-commissioned Forrester Study to understand the key cost drivers of mobile app delivery -- for both customer-facing and enterprise applications. Find out how you could lower costs and increase success with the right strategy and investment.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date