Click here to Skip to main content
15,880,651 members
Articles / Desktop Programming / MFC

Trouble-free Network Communication - XNet

Rate me:
Please Sign up or sign in to vote.
3.06/5 (7 votes)
12 Feb 2006CPOL2 min read 52.8K   436   20   16
Want a quick solution to setting up basic network communication between two machines? Check XNet out...

Introduction

Over the years, there's always this problem that I face whenever I need to share data between systems or machines over the network - the freely available networking libraries and code snippets are either sometimes broken or I need to read up a lot before I can make sense out of them. So I've decided to write a simple and flexible networking class for everyone to use.

Cool Features

Since it's simple and meant to get you started in minutes, you won't be seeing a long list of features. But do take note of its ease-of-use and convenience in terms of the data handling and setup.

  • Just one object for networking (either client or server)
  • Easy selection of UDP or TCP mode
  • No need for explicit data serialization before sending/retrieval; takes any data structure

Using the Code

It's easy to see how XNet can be integrated into any application in minutes. The only thing you have to do other than choosing where you want to create the XNet object and placing the sending/receiving functions is to define your own structure for the data you want to transmit before compilation. This can be done in MyDataStruct.h. XNet automatically handles the serialization for you by pointer "casting" and then reverses it at the receiving end. Check out the source codes for the XNet class for more information.

C++
// ... Headers and verbose ...
#include "XNet.h"

// Choose build options from the configurations manager tab in Visual Studio

int main(void)
{
 MyDataStruct ds, dr;
 ds.data[0]=10.f;
 ds.data[1]=20.f;
 ds.data[2]=30.f;
 ds.data[3]=40.f;
 ds.data[4]=50.f;
 ds.data[5]=60.f;

 XNet* xnet = XNet::Instance();

#ifdef TCP_SERVER
 xnet->init( SERVER, 2006, TCP );

 dr = xnet->receiveData();
 std::cout << "Random integrity check: " << dr.data[2] << std::endl;

 xnet->sendData( ds );
 std::cout << "Sent!" << std::endl;
#endif

#ifdef TCP_CLIENT
 xnet->init( CLIENT, 2006, TCP, "localhost" );

 xnet->sendData( ds );
 std::cout << "Sent!" << std::endl;

 dr = xnet->receiveData();
 std::cout << "Random integrity check: " << dr.data[5] << std::endl;
#endif

#ifdef UDP_SERVER
 xnet->init( SERVER, 2006, UDP );

 dr = xnet->receiveData();
 std::cout << "Random integrity check: " << dr.data[2] << std::endl;

 xnet->sendData( ds );
 std::cout << "Sent!" << std::endl;
#endif

#ifdef UDP_CLIENT
 xnet->init( CLIENT, 2006, UDP, "localhost" );

 xnet->sendData( ds );
 std::cout << "Sent!" << std::endl;

 dr = xnet->receiveData();
 std::cout << "Random integrity check: " << dr.data[5] << std::endl;
#endif

 getchar();  // Just a pause
 delete xnet; // Safe singleton deletion

 return 0;
}

Points of Interest

I have even built XNet as Matlab Simulink blocks due to the need in a piece of research work. XNet provides an easy mode of communication between Matlab and an external C++ console-based application without the hassle of network setup as in the case of using other networking blocks or libraries. For those who are interested, you can contact me for the Simulink blocks.

History

  • 12th February, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Singapore Singapore
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalshape recognition Pin
izzah_mustapha2-Mar-06 3:48
izzah_mustapha2-Mar-06 3:48 
GeneralRe: shape recognition Pin
Gabriyel2-Mar-06 4:01
Gabriyel2-Mar-06 4:01 
GeneralRe: shape recognition Pin
izzah_mustapha2-Mar-06 15:55
izzah_mustapha2-Mar-06 15:55 
GeneralRe: shape recognition Pin
Gabriyel2-Mar-06 18:35
Gabriyel2-Mar-06 18:35 
GeneralRe: shape recognition Pin
izzah_mustapha2-Mar-06 19:12
izzah_mustapha2-Mar-06 19:12 
what type of extension file is the .PPM?

GeneralRe: shape recognition Pin
Gabriyel2-Mar-06 22:11
Gabriyel2-Mar-06 22:11 
GeneralRe: shape recognition Pin
izzah_mustapha12-Mar-06 19:17
izzah_mustapha12-Mar-06 19:17 
GeneralRe: shape recognition Pin
izzah_mustapha12-Mar-06 21:02
izzah_mustapha12-Mar-06 21:02 
GeneralRe: shape recognition Pin
Gabriyel12-Mar-06 21:42
Gabriyel12-Mar-06 21:42 
GeneralRe: shape recognition Pin
izzah_mustapha12-Mar-06 21:52
izzah_mustapha12-Mar-06 21:52 
GeneralRe: shape recognition Pin
Gabriyel13-Mar-06 1:43
Gabriyel13-Mar-06 1:43 
GeneralQuestions Pin
Igor Vigdorchik12-Feb-06 16:53
Igor Vigdorchik12-Feb-06 16:53 
GeneralRe: Questions Pin
Gabriyel12-Feb-06 18:45
Gabriyel12-Feb-06 18:45 
GeneralRe: Questions Pin
Igor Vigdorchik13-Feb-06 3:22
Igor Vigdorchik13-Feb-06 3:22 
GeneralRe: Questions Pin
Chiew Heng Wah14-Feb-06 14:44
Chiew Heng Wah14-Feb-06 14:44 
GeneralRe: Questions Pin
Gabriyel14-Feb-06 17:09
Gabriyel14-Feb-06 17:09 

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.