File Exchange

image thumbnail


version (65.9 KB) by Giampiero Campa
Simulink Blocks for UDP/IP communication


Updated 01 Apr 2009

View License

These two blocks allow exchanging data bytes via an UDP/IP connection between simulink schemes running possibly as different processes on different (windows) machines.

Differently from the TCP/IP protocol, UDP/IP is totally asyncronous and non-blocking thus allowing in general a faster communication. Additionally, the client and server can be started or stopped in any order, as opposite to the TCP/IP case, where the server must always be started before the client is started and stopped after the client is stopped.

Finally, note that you can use the "byteview" block, available under
MATLAB Central > File Exchange > Utilities > Simulink, to transform any simulink data type into bytes (aka uint8) and back.

The C code for both the sender (client) and the receiver (server) is included in the zip file.

Cite As

Giampiero Campa (2020). udpip (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (5)

In order to make it not freeze under linux the socket must be set to non-blocking: line 169:
must be set to:
serv_sock = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0);
SOCK_NONBLOCK is linux specific so use a #if #else wrapper to make it copile for other sytems
Works well on OSx otherwise

Ryan Carter

This worked the first time. Unlike the UDP blocks available in Simulink, I like that this receive block will return the last valid data until a new packet is received. That is exactly what I needed.

Hey Giampiero Campa

Do you know if this can be converted to work with doubles instead of int8?


There is an error in the code of SimUdpServer3.c
In function mdlOutputs, the variable req_len is declared but never initialized or subsequently set to any value. Just before switch(info->connState), a line like:
req_len = sizeof(struct sockaddr_in);
needs to be added. Otherwise the recvfrom function will fail every time and you won't get any data.

Jaganathan Ranganathan


I'm trying to receive data from a specific ip address but when i used your udprecv algorithm, I'm not receiving any datas. It seems like it is not binding the data. Can you help me, how to receive the data.


I've inserted the line:
req_len = sizeof(struct sockaddr_in);
in SimUdpServer3.c (Thanks Matt).

Recompiled the s-functions with matlab 7.1 so that everything works fine with newer versions requiring the .mexw32 extension. The .dll files are retained to the benefit of users working with versions prior to 7.1, other users may delete them.

MATLAB Release Compatibility
Created with R11
Compatible with any release
Platform Compatibility
Windows macOS Linux