I am making a UDP server using Matlab to communicate with UDP client using Tiva C (by code composer) through ethernet cable , the client and server are on the same laptop but the tiva c IP address is 192.168.1.200

46 views (last 30 days)
1- i know that the udp server needs to specify its input(receiving) port
2- so when i initialize the server socket i write
udpA = udp('LocalPort',7070);
there is an error:
Unsuccessful open: Unknown RemoteHost: LocalPort.
i know that i shouldnot put the client id or port when initializing the server socket , because when the client intiates the communication ,it will automatically send its ip and port numbers to the server , so the server can send back to the client if needed
here is my code for the server:
udpA = udp('LocalPort',7070);
udpA.EnablePortSharing = 'on';
fopen(udpA)
if (udpA.BytesAvailable>0)
x = fread(udpA,udpA.BytesAvailable);
fprintf(udpA,'ok');
end

Answers (1)

Nathan Hardenberg
Nathan Hardenberg on 29 May 2021
Here is an example using udpport() since udp() will be removed in a future release!
Here PORT is the port of the server the data should be send to. The script waits infinitely on data. If a udp-packet was recived the data, sender adress and the sender port get read and extracted.
Here I decided to break out of the loop and send the same data back to the sender. You could also send back data inside the if statement and stay in the loop to have an ongoing udp-Server. Obviously you could also edit the recived data.
It's also worth to take a look at echoudp() since it does esentially the same thing! But to my knowledge you can not edit the data it just retruns the same data to the sender.
PORT = 7070 % port of this server
u = udpport("datagram","IPV4","LocalPort", PORT)
while true
if (u.NumDatagramsAvailable > 0) % if udp was received
% read udp
datagram = read(u, u.NumDatagramsAvailable, "uint8")
% get datagram components
data = datagram.Data
senderAdress = datagram.SenderAddress
senderPort = datagram.SenderPort
break
end
end
% send back the same data to the client
write(u, data, "uint8", senderAdress, senderPort);
clear u

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!