Click here to Skip to main content
15,891,473 members
Articles / Programming Languages / C++
Article

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

Rate me:
Please Sign up or sign in to vote.
4.00/5 (6 votes)
1 Nov 20073 min read 36.1K   1.1K   18   2
Demonstrates how to use a mailslot to broadcast information over a network.
Screenshot - mslot03.jpg

Introduction

This simple article will demonstrate how a client application can find its server in a network by using a Windows feature called Mailslot.

A Server which Broadcasts its IP and PORT

Every TCP/IP client application requires server IP and PORT to get connected to it. However, when server IP and/or PORT changes, all clients must be aware, otherwise they cannot communicate anymore.

How Can We Make a Server Broadcast Data to a Network?

Windows platform offers a mechanism called Mailslot which 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

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

C++
#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 recreating 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 as shown below:

Image 2
Figure 1

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

CreateFile opens, in this case, not a file but a mailslot name \\\\*\\mailslot\\@_MSClient_@. Notice the name convention: * means all computers which are reading a mailslot named \\\\.\\mailslot\\@_MSClient_@. Then, 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.

Image 3
Figure 2

On starting msclient (client), you will see the following:

Image 4
Figure 3

Each client instance creates a mailslot named \\\\.\\mailslot\\@_MSClient_@ and starts reading it (in fact, you can have one process using that name each time - see the msclient.cpp to see 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 a time.

So remember:

Writing to a MailslotCreateFileWriteFile
Reading from a MailslotCreateMailslotReadFile

Enjoy. Hope this helps.

History

  • First version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Brazil Brazil
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralClient Detecting Server Pin
reddy0717-Jun-09 4:54
reddy0717-Jun-09 4:54 
GeneralRe: Client Detecting Server Pin
Ciro Sisman Pereira14-Jul-09 18:39
Ciro Sisman Pereira14-Jul-09 18:39 

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.