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.
|
|
using Apewer; using Apewer.Internals; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading;
namespace Apewer.Network {
/// <summary>TCP 客户端。</summary>
public class TcpClient : IDisposable {
Socket _client = null;
/// <summary>套接字实例。</summary>
public Socket Socket { get => _client; }
/// <summary>已连接。</summary>
public bool Connected { get => _client != null && _client.Connected; }
/// <summary>启动客户端,并连接到服务端。</summary>
public TcpClient(string ip, int port) : this(IPAddress.Parse(ip), port) { }
/// <summary>启动客户端,并连接到服务端。</summary>
public TcpClient(IPAddress address, int port) : this(new IPEndPoint(address, port)) { }
/// <summary>启动客户端,并连接到服务端。</summary>
public TcpClient(IPEndPoint endpoint) { _client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _client.Connect(endpoint); _client.SendTimeout = 5000; }
/// <summary>关闭连接,释放系统资源。</summary>
public void Dispose() { try { _client.Disconnect(false); } catch { } try { _client.Close(100); } catch { } #if !NET20
try { _client.Dispose(); } catch { } #endif
}
/// <summary>接收。</summary>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentOutOfRangeException"></exception>
/// <exception cref="SocketException"></exception>
/// <exception cref="ObjectDisposedException"></exception>
/// <exception cref="System.Security.SecurityException"></exception>
public byte[] Receive(int maxLength = 1024) => _client.Receive(maxLength);
/// <summary>发送。</summary>
/// <exception cref="ArgumentNullException" />
/// <exception cref="SocketException" />
/// <exception cref="ObjectDisposedException" />
public void Send(byte[] data) => _client.Send(data);
}
}
|