Measurement Studio for VC++

Showing results for 
Search instead for 
Did you mean: 

Datasocket connection check



I have created a simple application that reads datasockets from datasocket server using Measurement Studio for C++. Here is the piece of my code:


code DSRead::Connect(char* url)


m_DataSocket.InstallEventHandler(*this, &DSRead:Smiley Very HappyataUpdatedEventHandler);

m_DataSocket.InstallEventHandler (*this, &DSRead:Smiley FrustratedtatusUpdatedEventHandler);

CNiString targetURL = url; 

m_DataSocket.Connect(targetURL, CNiDataSocket::ReadAutoUpdate);

MSG msg; 



while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))





connectionStatus = m_DataSocket.Status;


while ((connectionStatus != CNiDataSocket::ConnectionError)

&& (connectionStatus != CNiDataSocket::Unconnected));

if (connectionStatus == CNiDataSocket::ConnectionError)




return OK;



In DataUpdatedEventHandler function I process received data.

The do-while construction is used because the CNiDataSocket Connect function requires that application processes Windows messages while the DataSocket is connecting (as is explained in this post).

This works fine while connection to the datasocket server is alive. However, when connection is lost, nothing is changed. I mean,  m_DataSocket.IsConnected() is still true and connectionStatus is still CNiDataSocket::ConnectionActive. Moreover, if connection to DataSocket server reappears, the data is still not retrieved. 

The question is: How can loss of connection be detected in this case?

I have a feeling, that some windows functions should be used, but I failed to find answer in internet.

0 Kudos
Message 1 of 9

I have forgotten to mention, that Writer and DS Server are located on virtual machine while Reader is located on host.


In case when all three actors (Writer, DS Server and Reader) are located on virtual machine, and I turn off network connection, the problem doesn't arise: the StatusUpdatedEventHandler immediately reports about lost connection.

0 Kudos
Message 2 of 9

Hi mkalinkin,


I found two C++ forums online that addresses DataSocket connection issue. They are listed below for your reference.


I hope this helps.


Josh L.

Applications Engineer
National Instruments
0 Kudos
Message 3 of 9

Hi Josh,


Thank you for the reply. These two links, as far as I understand, are about Windows socket connection issue rather than DataSocket connection issue. Of course, one solution to this problem is to periodically send winsocket echo requests to DS server, but I believe it is not the optimal one.


Probably I have explained the problem ambiguously.

I will try to explain again, taking as an example "Simple DataSocket" project from "Visual C++ Examples" of Measurement Studio. In this example there are 3 actors: Writer, DS Server and Reader. I have a virtual machine which is connected to host in "host only" mode and has ip address The only thing I change in the code of "Simple DataSocket" example is url: I change "dstp://localhost/wave" to "dstp://" for both Reader and Writer.

Then I launch Reader and Writer in 2 scenarios:


1) I launch Writer, DS Server and Reader on virtual machine. Reader and Writer operate successfully: Writer writes data to DS server, Reader reads data. Both statuses are "Active:Connected".




Then I disable network adapter of virtual machine. As is expected, we have an error both for Writer and Reader:




2) I launch Writer and DS Server on virtual machine, and I launch Reader on the host. Reader and Writer also operate successfully: statuses are "Active:Connected". But if I disable network adapter of virtual machine, then the situation is different from first scenario:




I.e., Writer detects network disconnection, while Reader is still "Active:Connected", while really it isn't. As I have already written, m_DataSocket.IsConnected() property is also true.


The similar problem arises even if there is no virtual machine: when Writer and DS Server are on host A and Reader is in host B and if I unplug network cable for host A. Then again Reader cannot detect connection fail.

Basically the question is: how automatically it is possible to detect fail of connection in such situations?


I will explain why I ask this question. I have a simulation program that generates data and publishs it to DS Server (both are located on virtual machine). I also have Reader application located on host. I simulate network fail by disabling network adapter for virtual machine. I want Reader to automatically detect network fail and generate an error. As I have explained above, currently Reader does not detect it.

0 Kudos
Message 4 of 9

Hi mkalinkin,


I tried to reproduce your issue with our C# shipping example on my end. Unfortunately, I was not able to reproduce what you see on your end. The C++ example should be very similar to the C++ code. I'm going to run the C++ examples later and give you an update.




Josh L. 

Applications Engineer
National Instruments
0 Kudos
Message 5 of 9

Just an update, I haven't forgotten about you yet. We no longer support C++ applications. I haven't found a time to install the older version of Visual Studio on our test machines. I promise to have something ready for you by the end of this week.

Applications Engineer
National Instruments
0 Kudos
Message 6 of 9

Hello Josh,


Thank you for support! I will wait for your message.



0 Kudos
Message 7 of 9

Hi Mikhail,


Thank you for your patience with this issue. I was able to test the datasocket this weekend on one of our test machines. Unfortunately, I was still not able to reproduce the error you see. I just disabled the datasocket server on the write machine via task manager. That might be the only difference between our methods. I wonder if the behavior you see is only true with virtual machines. Can you test the datasocket with another computer? I do not believe you will see this error then.




Josh L.

Applications Engineer
National Instruments
0 Kudos
Message 8 of 9

Hi Josh,


Thank you for support and for working on weekends=)


As you've proposed, I have tested datasocket Reader and Writer programs without virtual machines.

The situation is following. On the host A there are DS server and Writer. On the host B there is Reader.

I have performed 3 experiments:

1) I terminate Writer using task manager. In this case I successfully detect connection error both with Reader and Writer.

2) I unplug network cable from host B (that hosts Reader). I again successfully detect connection error both with Reader and Writer.

3) I unplug network cable from host A (that hosts DS Server and Writer). Writer detects connection error, but Reader still shows "Active:Connected".


From these 3 experiments one may conclude, that datasocket Reader application evaluates connection status not by reachability of DS Server, but by reachability of the network card. This explains why in experiment 2 Reader detects connection error, while in experiment 3 - doesn't detect.


Is this conclusion correct? If yes, are there any methods from NI to automatically detect problems with rechability of DS server

I need this method for scenarious like in experiment 3 - when suddenly there is network error with machine that hosts datasocket Reader application.

The solution "from time to time ping machine with DS Server" is not convenient for me since a lot of time is wasted for sending and receiving packets.


Best regards,



0 Kudos
Message 9 of 9