SocketClient.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. 
  2. using Invengo.Foundation.Util;
  3. using log4net;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Configuration;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Net.Sockets;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace GDNXFD.WcfService
  13. {
  14. public class SocetClient
  15. {
  16. #region 单例方法
  17. public static SocetClient Instance
  18. {
  19. get { return SingletonCreator.instance; }
  20. }
  21. class SingletonCreator
  22. {
  23. internal static readonly SocetClient instance = new SocetClient();
  24. }
  25. #endregion
  26. private static ILog logger = LogManager.GetLogger("AppInfoLog");
  27. private IPAddress _serverAddress = IPAddress.Parse("172.168.1.19");
  28. private int _serverPort = 5050;
  29. private Socket _sock;
  30. private void Connect()
  31. {
  32. try
  33. {
  34. if (_sock == null)
  35. {
  36. _sock = new Socket(_serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  37. }
  38. if (_sock.Connected)
  39. return;
  40. _sock.Connect(_serverAddress, _serverPort);
  41. }
  42. catch (Exception ex)
  43. {
  44. logger.WarnFormat("连接服务器失败!错误原因:{0}", ex.Message);
  45. _sock.Close();
  46. _sock = null;
  47. }
  48. }
  49. private void ProcessConnect(IAsyncResult ar)
  50. {
  51. if (_sock != null && _sock.Connected)
  52. {
  53. logger.Info("连接服务器成功!");
  54. //_sock.BeginReceive(_receiveBuffer, 0, MAX_BUFFER_SIZE, SocketFlags.None, ProcessReceive, null);
  55. }
  56. else
  57. {
  58. logger.WarnFormat("连接服务器失败!");
  59. CloseSocket();
  60. }
  61. }
  62. private void CloseSocket()
  63. {
  64. if (_sock != null)
  65. {
  66. //_sock.Shutdown(SocketShutdown.Send);
  67. _sock.Close();
  68. _sock = null;
  69. logger.Info("连接断开!");
  70. }
  71. }
  72. public void Send(byte[] msgBody)
  73. {
  74. byte[] frameB = new byte[] { 0x5a, 0x5a, 0x01, 0x01 };
  75. byte[] lenB = BitConverter.GetBytes((short)(msgBody.Length + 6));
  76. byte[] buffer = new byte[msgBody.Length + 6];
  77. Array.Copy(frameB, buffer, 4);
  78. Array.Copy(lenB, 0, buffer, 4, 2);
  79. Array.Copy(msgBody, 0, buffer, 6, msgBody.Length);
  80. Connect();
  81. if (_sock == null || !_sock.Connected)
  82. {
  83. logger.Info("发送失败!尚未建立与服务器的连接");
  84. CloseSocket();
  85. return;
  86. }
  87. try
  88. {
  89. _sock.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, ProcessSend, buffer);
  90. }
  91. catch (Exception ex)
  92. {
  93. logger.ErrorFormat("发送失败!异常:{0}", ex.Message);
  94. }
  95. }
  96. private void ProcessSend(IAsyncResult iar)
  97. {
  98. byte[] buffer = (byte[])(iar.AsyncState);
  99. string hexString = CodeConvert.BytesToHexString(buffer, " ");
  100. logger.Info("发送数据:" + hexString);
  101. }
  102. }
  103. }