MCSocket.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Aitex.Core.RT.Log;
  9. namespace MECF.Framework.RT.Core.IoProviders
  10. {
  11. class MCSocket
  12. {
  13. private int m_nPort;
  14. private string m_strAddress;
  15. private Socket m_socket;
  16. private int m_nTimeOut;
  17. public MCSocket()
  18. {
  19. m_nTimeOut = 30000;
  20. //m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  21. }
  22. ~MCSocket()
  23. {
  24. }
  25. public bool Connected
  26. {
  27. get { return (m_socket != null && m_socket.Connected); }
  28. }
  29. public bool Open(string strAddress, int nPort, string strLocalAddress)
  30. {
  31. if (Connected)
  32. return true;
  33. Close();
  34. m_strAddress = strAddress;
  35. m_nPort = nPort;
  36. m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  37. try
  38. {
  39. // Bind to specific local endpoint
  40. IPAddress localAddress = IPAddress.Parse(strLocalAddress);
  41. IPEndPoint localEndPoint = new IPEndPoint(localAddress, 0);
  42. m_socket.Bind(localEndPoint);
  43. IPAddress ipAddress = IPAddress.Parse(m_strAddress);
  44. IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, m_nPort);
  45. //m_socket = new Socket(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  46. m_socket.SendTimeout = m_nTimeOut;
  47. m_socket.ReceiveTimeout = m_nTimeOut;
  48. m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
  49. m_socket.Connect(ipEndPoint);
  50. }
  51. catch (Exception ex)
  52. {
  53. LOG.Write(ex);
  54. return false;
  55. }
  56. return true;
  57. }
  58. public bool Close()
  59. {
  60. if (m_socket == null) return true;
  61. try
  62. {
  63. m_socket.Shutdown(SocketShutdown.Both);
  64. m_socket.Close();
  65. m_socket = null;
  66. }
  67. catch (Exception ex)
  68. {
  69. LOG.Write(ex);
  70. return false;
  71. }
  72. return true;
  73. }
  74. public bool Read(byte[] buffer)
  75. {
  76. if (!Connected) return false;
  77. try
  78. {
  79. m_socket.Receive(buffer);
  80. }
  81. catch (Exception ex)
  82. {
  83. LOG.Write(ex);
  84. return false;
  85. }
  86. return true;
  87. }
  88. public bool Read(byte[] buffer, int length)
  89. {
  90. if (!Connected) return false;
  91. try
  92. {
  93. if (length < 0) return false;
  94. m_socket.Receive(buffer, length, SocketFlags.None);
  95. }
  96. catch (Exception ex)
  97. {
  98. LOG.Write($"Receive data failed, {ex.Message}, from {m_strAddress}:{m_nPort}");
  99. return false;
  100. }
  101. return true;
  102. }
  103. public bool Write(byte[] buffer)
  104. {
  105. if (!Connected) return false;
  106. try
  107. {
  108. m_socket.Send(buffer, SocketFlags.None);
  109. }
  110. catch (Exception ex)
  111. {
  112. LOG.Write(ex);
  113. return false;
  114. }
  115. return true;
  116. }
  117. public bool Write(byte[] buffer, int length)
  118. {
  119. if (!Connected) return false;
  120. try
  121. {
  122. m_socket.Send(buffer, length, SocketFlags.None);
  123. }
  124. catch (Exception ex)
  125. {
  126. LOG.Write(ex);
  127. return false;
  128. }
  129. return true;
  130. }
  131. }
  132. }