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