NetHelper.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.Security;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. namespace ErgometerLibrary
  8. {
  9. public class NetHelper
  10. {
  11. public static bool SendNetCommand(TcpClient client, NetCommand command)
  12. {
  13. /*
  14. byte[] b = Encoding.Unicode.GetBytes(command.ToString());
  15. client.GetStream().Write(b, 0, b.Length);
  16. client.GetStream().Flush();
  17. */
  18. return SendString(client, command.ToString());
  19. }
  20. public static bool SendString(TcpClient client, string command)
  21. {
  22. /*
  23. byte[] b = Encoding.Unicode.GetBytes(command);
  24. client.GetStream().Write(b, 0, b.Length);
  25. client.GetStream().Flush();
  26. */
  27. if (client != null && client.Connected)
  28. {
  29. try
  30. {
  31. StreamWriter wr = new StreamWriter(client.GetStream(), Encoding.Unicode);
  32. wr.WriteLine(AESEncrypt.EncryptToString(command));
  33. wr.Flush();
  34. return true;
  35. }
  36. catch (Exception e)
  37. {
  38. return false;
  39. }
  40. }
  41. else
  42. return false;
  43. }
  44. public static NetCommand ReadNetCommand(TcpClient client)
  45. {
  46. /*
  47. byte[] bytesFrom = new byte[(int) client.ReceiveBufferSize];
  48. client.GetStream().Read(bytesFrom, 0, (int)client.ReceiveBufferSize);
  49. string response = Encoding.Unicode.GetString(bytesFrom);
  50. NetCommand net = NetCommand.Parse(response);
  51. return net;
  52. */
  53. string str;
  54. NetCommand net;
  55. str = ReadString(client);
  56. if (str != "")
  57. {
  58. try
  59. {
  60. str = AESEncrypt.DecryptString(str);
  61. }
  62. catch (Exception e)
  63. {
  64. str = "";
  65. net = new NetCommand(NetCommand.CommandType.ERROR, 0);
  66. }
  67. try {
  68. net = NetCommand.Parse(str);
  69. }
  70. catch (Exception e)
  71. {
  72. net = new NetCommand(NetCommand.CommandType.ERROR, 0);
  73. }
  74. }
  75. else
  76. net = new NetCommand(NetCommand.CommandType.ERROR, 0);
  77. return net;
  78. }
  79. public static string ReadString(TcpClient client)
  80. {
  81. /*
  82. byte[] bytesFrom = new byte[(int)client.ReceiveBufferSize];
  83. client.GetStream().Read(bytesFrom, 0, (int)client.ReceiveBufferSize);
  84. string response = Encoding.Unicode.GetString(bytesFrom);
  85. return response;
  86. */
  87. if (client != null && client.Connected)
  88. {
  89. try {
  90. StreamReader rd = new StreamReader(client.GetStream(), Encoding.Unicode);
  91. string str = rd.ReadLine();
  92. return str;
  93. }
  94. catch(Exception e)
  95. {
  96. return "";
  97. }
  98. }
  99. else
  100. return "";
  101. }
  102. public static IPAddress GetIP(string ipstring)
  103. {
  104. IPAddress ip;
  105. bool ipIsOk = IPAddress.TryParse(ipstring, out ip);
  106. if (!ipIsOk) { return null; }
  107. return ip;
  108. }
  109. public static IPAddress GetIP()
  110. {
  111. IPHostEntry host;
  112. string localIP = "?";
  113. host = Dns.GetHostEntry(Dns.GetHostName());
  114. foreach (IPAddress ip in host.AddressList)
  115. {
  116. if (ip.AddressFamily == AddressFamily.InterNetwork)
  117. {
  118. localIP = ip.ToString();
  119. }
  120. }
  121. return GetIP(localIP);
  122. }
  123. }
  124. }