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.

107 lines
4.8 KiB

  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. namespace Apewer.Network
  5. {
  6. /// <summary>TCP 客户端。</summary>
  7. public class TcpClient : IDisposable
  8. {
  9. Socket _socket = null;
  10. /// <summary>套接字实例。</summary>
  11. public Socket Socket { get => _socket; }
  12. /// <summary>在线。</summary>
  13. public bool Online { get => NetworkUtility.Online(_socket); }
  14. /// <summary>本地终结点。</summary>
  15. public IPEndPoint LocalEndPoint { get; private set; }
  16. /// <summary>远程终结点。</summary>
  17. public IPEndPoint RemoteEndPoint { get; private set; }
  18. /// <summary>启动客户端,并连接到服务端。</summary>
  19. /// <param name="ip">远程 IP 地址。</param>
  20. /// <param name="port">远程端口号。</param>
  21. /// <exception cref="ArgumentOutOfRangeException" />
  22. /// <exception cref="FormatException" />
  23. /// <exception cref="SocketException" />
  24. public TcpClient(string ip, int port) : this(new IPEndPoint(IPAddress.Parse(ip), port), 0) { }
  25. /// <summary>启动客户端,并连接到服务端。</summary>
  26. /// <param name="ip">远程 IP 地址。</param>
  27. /// <param name="port">远程端口号。</param>
  28. /// <param name="timeout">连接超时毫秒数。当达到指定时长,或达到系统默认时长时,将会发生超时异常。</param>
  29. /// <exception cref="ArgumentOutOfRangeException" />
  30. /// <exception cref="FormatException" />
  31. /// <exception cref="SocketException" />
  32. public TcpClient(string ip, int port, int timeout) : this(new IPEndPoint(IPAddress.Parse(ip), port), timeout) { }
  33. /// <summary>启动客户端,并连接到服务端。</summary>
  34. /// <param name="ip">远程 IP 地址。</param>
  35. /// <param name="port">远程端口号。</param>
  36. /// <exception cref="ArgumentNullException" />
  37. /// <exception cref="ArgumentOutOfRangeException" />
  38. /// <exception cref="SocketException" />
  39. public TcpClient(IPAddress ip, int port) : this(new IPEndPoint(ip, port), 0) { }
  40. /// <summary>启动客户端,并连接到服务端。</summary>
  41. /// <param name="ip">远程 IP 地址。</param>
  42. /// <param name="port">远程端口号。</param>
  43. /// <param name="timeout">连接超时毫秒数。当达到指定时长,或达到系统默认时长时,将会发生超时异常。</param>
  44. /// <exception cref="ArgumentNullException" />
  45. /// <exception cref="ArgumentOutOfRangeException" />
  46. /// <exception cref="SocketException" />
  47. public TcpClient(IPAddress ip, int port, int timeout) : this(new IPEndPoint(ip, port), timeout) { }
  48. /// <summary>启动客户端,并连接到服务端。</summary>
  49. public TcpClient(IPEndPoint endpoint) : this(endpoint, 0) { }
  50. /// <summary>启动客户端,并连接到服务端。</summary>
  51. /// <param name="endpoint">远程终结点。</param>
  52. /// <param name="timeout">连接超时毫秒数。当达到指定时长,或达到系统默认时长时,将会发生超时异常。</param>
  53. /// <exception cref="ArgumentNullException" />
  54. /// <exception cref="ArgumentOutOfRangeException" />
  55. /// <exception cref="SocketException" />
  56. public TcpClient(IPEndPoint endpoint, int timeout)
  57. {
  58. if (endpoint == null) throw new ArgumentNullException(nameof(endpoint));
  59. if (timeout < 1) throw new ArgumentOutOfRangeException(nameof(timeout));
  60. _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  61. _socket.Connect(endpoint, timeout);
  62. _socket.SendTimeout = 5000;
  63. LocalEndPoint = _socket.LocalEndPoint as IPEndPoint;
  64. RemoteEndPoint = endpoint;
  65. }
  66. /// <summary>关闭连接,释放系统资源。</summary>
  67. public void Dispose()
  68. {
  69. try { _socket.Disconnect(false); } catch { }
  70. try { _socket.Close(100); } catch { }
  71. #if !NET20
  72. try { _socket.Dispose(); } catch { }
  73. #endif
  74. }
  75. /// <summary>接收。</summary>
  76. /// <exception cref="ArgumentNullException"></exception>
  77. /// <exception cref="ArgumentOutOfRangeException"></exception>
  78. /// <exception cref="SocketException"></exception>
  79. /// <exception cref="ObjectDisposedException"></exception>
  80. /// <exception cref="System.Security.SecurityException"></exception>
  81. public byte[] Receive(int maxLength = 1024) => _socket.Receive(maxLength);
  82. /// <summary>发送。</summary>
  83. /// <exception cref="ArgumentNullException" />
  84. /// <exception cref="SocketException" />
  85. /// <exception cref="ObjectDisposedException" />
  86. public void Send(byte[] data) => _socket.Send(data);
  87. }
  88. }