I'm on OS X. I have a MIDI keyboard plugged in. In Audio MIDI Setup, I redirect MIDI events from the keyboard onto the network. it tells me it is using port 5004:
NOTE: Notice I have set 'who may connect me' to 'anyone', and in 'live routings' I have sent the MIDI keyboard routing INTO the network.
Now from a C#/.NET script (within my Unity3D project), I attempt to listen to this port:
UdpClient udp;
udp = new UdpClient( 5004 ) // <-- SocketException: Address already in use
System.Object myObj = null;
udp.BeginReceive(
new AsyncCallback( UDP_IncomingData ),
myObj // udp_ep
);
static void UDP_IncomingData( IAsyncResult ar )
{
Debug.Log( "GotData" );
//Get the data from the response
byte[] bResp = udp.EndReceive( ar, ref udp_ep );
//Convert the data to a string
string sResponse = Encoding.UTF8.GetString( bResp );
//Display the string
Console.WriteLine( sResponse );
//Close the UDP connection
udp.Close( );
}
I can't understand this error message. Of course the port is in use; the MIDI keyboard should be broadcasting from this port.
How do I listen to the broadcast?
EDIT: I'm now thinking that I need to create the UDP client on a new unused port, and then connect to the port I'm interested in:
udp = new UdpClient( 0 ); // <-- http://stackoverflow.com/questions/2456773/find-a-free-port
udp.Connect( "192.168.0.13", 5004 );
So now it doesn't generate any errors. But I press keys on the MIDI keyboard, and the received-data callback doesn't trigger. So I still don't know if I've got it right.