OSCTransmitter.cs 1.2 KB

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