- Go to Enable Port Sharing over UDP section in link : https://www.mathworks.com/help/instrument/creating-a-udp-object.html
How do I read datagrams from a UDP port (that has been set up in matlab) in a mex file?
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have a C program which is sending packets of data from a large file over to a MATLAB program which is then reconstructing that file within MATLAB (i.e. writing the received data to a new file of the same name).
Currently, I have MATLAB receiving the data packets (very slowly - taking minutes not seconds) from C using a UDP port object and the read function (matlab.io.datastore.DsFileReader.read - Read bytes from file - MATLAB - this function retrieves the data that has been sent in the message from the C program and then this data is processed later on in the MATLAB program).
I think the 'read' function is slowing this down and so I want to implement this function as a MEX file to see if it speeds it up (my peer has similarly sped up the process of sending a file from MATLAB to C by replacing the send functionality with a MEX file C implementation).
I am unable to change the fact that the port is being set up in MATLAB (I can't do this in a MEX file) due to other dependencies in my MATLAB program. Obviously I cant create a socket in my MEX file using the same port number etc as I have already set this port up in MATLAB but I was wondering if there was a way for the MEX file to access the datagrams that are on this port without directly setting it up?
So far I have created a MEX file which successfully receives the UDP port object from MATLAB as an input (I have used mxGetClassName which returns udpport.datagram.UDPPort). How do I access the data which is being received at this port within my MEX file to be processed in the way that 'read' does? Is there a way of "converting" the UDP port into a winsock2 Socket object to do this?
0 comentarios
Respuestas (1)
Aryan
el 12 de Ag. de 2025
I understand that you want to speed up UDP datagram reading in MATLAB and are wondering if you can use a MEX file to do so, possibly by sharing the UDP port.
In recent MATLAB releases, you can enable port sharing on a udpport object using the "EnablePortSharing" property. This allows MATLAB and other applications (like your MEX file) to bind to the same UDP port and receive datagrams simultaneously.
How to do it:
• When creating your udpport object in MATLAB, set "EnablePortSharing" to true:
u = udpport("LocalPort", 3030, "EnablePortSharing", true);
• In your MEX file (written in C/C++), you can create and bind your own UDP socket to port 3030, making sure to set the appropriate socket options (like SO_REUSEADDR). Both MATLAB and your MEX file can now receive the same UDP datagrams (especially for broadcast/multicast traffic).
A few important notes:
• You are not directly accessing MATLAB’s UDP buffer; instead, your MEX file acts as a separate receiver on the same port.
• This approach works best with broadcast or multicast UDP traffic. For unicast, behavior may depend on your operating system.
• You cannot "replace" MATLAB’s read method, but you can bypass it by handling UDP reception and processing in your MEX file for better performance.
For more details, you can refer to the MathWorks documentation :
Hope this helps!
Ver también
Categorías
Más información sobre Instrument Control Toolbox Supported Hardware en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!