OSCTransmitter.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. namespace OSC.NET
  5. {
  6. /// <summary>
  7. /// OSCTransmitter
  8. /// </summary>
  9. public class OSCTransmitter
  10. {
  11. protected UdpClient udpClient;
  12. protected string remoteHost;
  13. protected int remotePort;
  14. public OSCTransmitter(string remoteHost, int remotePort)
  15. {
  16. this.remoteHost = remoteHost;
  17. this.remotePort = remotePort;
  18. Connect();
  19. }
  20. public void Connect()
  21. {
  22. if (this.udpClient != null) Close();
  23. this.udpClient = new UdpClient(this.remoteHost, this.remotePort);
  24. }
  25. public void Close()
  26. {
  27. this.udpClient.Close();
  28. this.udpClient = null;
  29. }
  30. public int Send(OSCPacket packet)
  31. {
  32. int byteNum = 0;
  33. byte[] data = packet.BinaryData;
  34. try
  35. {
  36. byteNum = this.udpClient.Send(data, data.Length);
  37. }
  38. catch (Exception e)
  39. {
  40. Console.Error.WriteLine(e.Message);
  41. Console.Error.WriteLine(e.StackTrace);
  42. }
  43. return byteNum;
  44. }
  45. }
  46. }