MATLAB Answers

sending data from Matlab to C++ over UDP

67 views (last 30 days)
Yuval Porat
Yuval Porat on 20 Jul 2018
Commented: kuan jiang on 28 May 2019
Hi all,
I'm trying to send data back and forth between a Matlab app and a C++ app. The data is comprised of a 100 char array. Both apps are on the same computer. The Matlab app in this case is the Server and the C++ is the client.
I am able to receive data properly in the direction from my C++ app to Matlab. However I am not able to receive the data in the opposite direction from Matlab to my C++ app, though I'm using exactly the same socket.
I checked my C++ app, by using a second C++ application that talks to the first one through the same UDP interface and the data was transmitted back and forth properly.
Just for testing the connection, I'm trying to send a 100 char array with 'A' in the first cell from C++ to Matlab, and return a 100 char array with 'I' in the first cell from Matlab to C++:
CPP_IP = '';
CPP_Port = 817;
OpenCPP = udp(CPP_IP,CPP_Port,'LocalPort',CPP_Port);
OpenCPP.InputBufferSize = 100;
OpenCPP.OutputBufferSize = 100;
OpenCPP.ByteOrder = 'littleEndian';
OpenCPP.Timeout = 30;
CPP_Connection = 0;
while ~CPP_Connection
fprintf('waiting for CPP to come alive...')
msg = fscanf(OpenCPP,'%c',[100 1]);
if OpenCPP.ValuesReceived == 100
if msg(1) == 'A'
fprintf('\nCPP alive and connected!\n')
CPP_Connection = 1;
fprintf('Command from CPP incomplete!')
clear OpenCPP
msg(1) = 'I';
When running this code and my C++ app, if I check the connection properties after receiving the letter 'A', this is what I get:
UDP Object : UDP-
Communication Settings
RemotePort: 817
Terminator: 'LF'
Communication State
Status: open
RecordStatus: off
Read/Write State
TransferStatus: idle
BytesAvailable: 100
ValuesReceived: 100
ValuesSent: 200
I received 100 chars as expected and sent 100 chars. It looks like the sent data is not actually read by the C++, although as I said, the interface on the C++ app was checked with another C++ app and worked fine.
It seems like an issue in the communication between Matlab and C++
Any help would be highly appreciated!


Sign in to comment.

Accepted Answer

Yuval Porat
Yuval Porat on 20 Jul 2018
Never mind. I realized what went wrong.
Though declaring the IP and Port of the socket at the beginning, the port in which data was sent from my C++ app to Matlab was randomly selected. I fixed it by reading in the new incoming port in fscanf:
[Buff,count,msg,datagramaddress,datagramport] = fscanf(OpenCPP,'%c',[100 1]);
Then, changing the port of my CPP connection to the new one using:
OpenCPP.RemotePort = datagramport;
and writing my data using fprintf:


Sign in to comment.

More Answers (1)

kuan jiang
kuan jiang on 27 May 2019
hello, i'm trying to make communnication between c++ app and matlab like you did,could you tell me how to realize it
thanks a lot.


Yuval Porat
Yuval Porat on 27 May 2019
Hi, if you need general instructions I suggest reading the Matlab documentation on tcp/udp communications
If you faced the same problem as mine, please read the answer I posted where I detailed the solution I found.
If you're having another problem, please be more specific and the community will try to help you.
kuan jiang
kuan jiang on 28 May 2019
Ah! thank you very much.Generally, i want to make co-simulation between MATLAB and ns-3 (a network simulator), and i read some papers found they using socket to combine these two simulators to realize data transfer from matlab to ns3 and back ,question is i don't know how the matlab use socket to transfer data to external software and get back data...
Thanks a lot.

Sign in to comment.





Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!