It's not clear to me what UDP port you want to send your data to, and what sort of process you're using to monitor and test your code. There are two ports involved... One is the local port that you're binding to (that's the argument to 'udpsocket'), and the other port is the destination port of the packet, given along with the destination host.
It appears to me that the Instrument Control Toolbox syntax requires the destination hostname and port during creation. It allows you to specify the local port as an optional argument if you want to. In constrast, pnet('udpsocket') takes the local port. You should notice in your first set of code that nothing specifies the destination host, which should make you suspicious... If you read on in the UDP docs of pnet.m, you'll see that there's another function: pnet(sock, 'udpconnect', 'hostname', port)
, which "connects" the UDP socket to the host/port pair, so that you can leave out the hostname/port when doing writepacket.
So here is what I think the equivalent is of your original Instrument Control Toolbox code:
sock=pnet('udpsocket',1237); % Does local port matter? You haven't said...
pnet(sock, 'udpconnect', '127.0.0.1', 12345); % Destination port
pnet(sock, 'writepacket', data);
You should ALWAYS use 'writepacket' for UDP, never 'write'. And note that you can either do 'udpconnect' as I've listed above, OR you can supply hostname/port with every writepacket, as you were trying to do.
What I can't figure out from your question is how 6351 enters in, as that never showed up in your reference code.