No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Client.cs 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Net.Sockets;
  3. using System.Threading;
  4. using System.Text;
  5. namespace monitor
  6. {
  7. public class ClientReadEvent
  8. {
  9. private static TcpClient myClient = null;
  10. private static NetworkStream myStream = null;
  11. private const int BufferMaxSize = 512;
  12. private static byte[] buffer = new byte[BufferMaxSize];
  13. private static StringBuilder sb = new StringBuilder();
  14. private static int newLength = 1;
  15. public delegate void ReadEvent(string str);
  16. public static ReadEvent readEvent = null;
  17. public static void Set(TcpClient client, NetworkStream stream)
  18. {
  19. myClient = client;
  20. myStream = stream;
  21. }
  22. public static void ReadThread()
  23. {
  24. while (true)
  25. {
  26. if (myClient.Connected)
  27. {
  28. myStream.BeginRead(buffer, 0, newLength, new AsyncCallback(ReadCallback), sb);
  29. }
  30. else Thread.Sleep(200);
  31. }
  32. }
  33. public static void ReadCallback(IAsyncResult ar)
  34. {
  35. }
  36. }
  37. public class Client
  38. {
  39. public const string defaultIP = "localhost";
  40. public const int defaultPort = 4500;
  41. private static TcpClient client = null;
  42. private static NetworkStream stream = null;
  43. private const int BufferMaxSize = 512;
  44. private static byte[] buffer = new byte[BufferMaxSize];
  45. private static StringBuilder message = new StringBuilder();
  46. private static int newLength = 1;
  47. public delegate void ReadEvent(string msg);
  48. public static ReadEvent readEvent = null;
  49. public Client()
  50. {
  51. }
  52. public static bool Open(string host)
  53. {
  54. return Client.Open(host, defaultPort);
  55. }
  56. public static bool Open(string host, int port)
  57. {
  58. bool status = true;
  59. try
  60. {
  61. client = new TcpClient(host, port);
  62. stream = client.GetStream();
  63. stream.BeginRead(buffer, 0, newLength, new AsyncCallback(ReadCallback), message);
  64. }
  65. catch (ArgumentNullException e)
  66. {
  67. Console.WriteLine("ArgumentNullException: " + e);
  68. status = false;
  69. }
  70. catch (SocketException e)
  71. {
  72. Console.WriteLine("SocketException: " + e.ToString());
  73. status = false;
  74. }
  75. catch (Exception e)
  76. {
  77. Console.WriteLine("Unknown Exception: " + e.ToString());
  78. status = false;
  79. }
  80. return status;
  81. }
  82. public static void Close()
  83. {
  84. if (stream!=null) stream.Close();
  85. if (client!=null) client.Close();
  86. }
  87. private static void ReadCallback(IAsyncResult ar)
  88. {
  89. if (client.Connected)
  90. {
  91. int bytesRead;
  92. try
  93. {
  94. bytesRead = stream.EndRead(ar);
  95. }
  96. catch (ObjectDisposedException e)
  97. {
  98. Console.WriteLine("Connection to server dropped: " + e.ToString());
  99. return;
  100. }
  101. newLength = 1;
  102. if (bytesRead > 0)
  103. {
  104. message.Append(Encoding.ASCII.GetString(buffer, 0, bytesRead));
  105. }
  106. if (client.Available > 0)
  107. {
  108. newLength = client.Available;
  109. if (newLength > BufferMaxSize) newLength = BufferMaxSize;
  110. else newLength = client.Available;
  111. }
  112. else
  113. {
  114. readEvent?.Invoke(message.ToString());
  115. message.Clear();
  116. }
  117. stream.BeginRead(buffer, 0, newLength, new AsyncCallback(ReadCallback), message);
  118. }
  119. }
  120. public static void Write(string mes)
  121. {
  122. if (client.Connected)
  123. {
  124. byte[] writeBuffer = Encoding.UTF8.GetBytes(mes);
  125. stream.Write(writeBuffer, 0, mes.Length);
  126. }
  127. }
  128. }
  129. }