SocketClient.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Ant.Service.Utility
  8. {
  9. public class SocketClient : Object
  10. {
  11. private TcpClient tcpClient;
  12. public SocketClient()
  13. {
  14. tcpClient = new TcpClient();
  15. tcpClient.Client.Blocking = true;
  16. }
  17. /// <summary>
  18. /// 连接
  19. /// </summary>
  20. /// <param name="host"></param>
  21. /// <param name="port"></param>
  22. public void Connect(string host, int port)
  23. {
  24. tcpClient.Connect(host, port);
  25. }
  26. /// <summary>
  27. /// 关闭连接
  28. /// </summary>
  29. public void Disconnect()
  30. {
  31. tcpClient.Close();
  32. }
  33. /// <summary>
  34. /// 发送数据
  35. /// </summary>
  36. /// <param name="json"></param>
  37. /// <returns></returns>
  38. public bool SendData(byte[] buffer)
  39. {
  40. var len = tcpClient.Client.Send(buffer, 0, buffer.Length, SocketFlags.None);
  41. return len > 0;
  42. }
  43. }
  44. }