试试 Peter Rydesäter 的工具箱:http: //www.mathworks.com/matlabcentral/fileexchange/345-tcpudpip-toolbox-2-0-6很旧但仍然有效。
具有两个 Matlab 实例的示例:
发件人:
function sendudp(host, port, data)
udp=pnet('udpsocket',1111); % open local socket on port 1111
pnet(udp,'write',data); % write data to buffer
pnet(udp,'writepacket',host,port); % send udp-packet
pnet(udp,'close'); % close connection
end
接收者:
sock = pnet('udpsocket', 950) % open socket on port 950
发件人:
sendudp('127.0.0.1', 950, 'Hello World') % Call function defined above, send Hello World to localhost, port 950
接收者:
size = pnet(sock, 'readpacket'); % Get Data from sock, and load to buffer
data = pnet(sock, 'read'); % Read Data from buffer