MCSocket.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.WriteExeption(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.WriteExeption(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.WriteExeption(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. m_socket.Receive(buffer, length, SocketFlags.None);
  94. }
  95. catch (Exception ex)
  96. {
  97. LOG.WriteExeption($"Receive data failed, from {m_strAddress}:{m_nPort}", ex);
  98. return false;
  99. }
  100. return true;
  101. }
  102. public bool Write(byte[] buffer)
  103. {
  104. if (!Connected) return false;
  105. try
  106. {
  107. m_socket.Send(buffer, SocketFlags.None);
  108. }
  109. catch (Exception ex)
  110. {
  111. LOG.WriteExeption(ex);
  112. return false;
  113. }
  114. return true;
  115. }
  116. public bool Write(byte[] buffer, int length)
  117. {
  118. if (!Connected) return false;
  119. try
  120. {
  121. m_socket.Send(buffer, length, SocketFlags.None);
  122. }
  123. catch (Exception ex)
  124. {
  125. LOG.WriteExeption(ex);
  126. LOG.WriteExeption(ex);
  127. return false;
  128. }
  129. return true;
  130. }
  131. }
  132. }