2 Commits

  1. 18
      Apewer/Network/Extensions.cs
  2. 41
      Apewer/Network/TcpClient.cs

18
Apewer/Network/Extension.cs → Apewer/Network/Extensions.cs

@ -9,7 +9,7 @@ namespace Apewer.Network
{
/// <summary>扩展方法。</summary>
public static class Extension
public static class Extensions
{
/// <summary>添加邮件账户。</summary>
@ -155,15 +155,31 @@ namespace Apewer.Network
return json;
}
/// <summary>检查 Socket 在线状态。</summary>
/// <exception cref="NotSupportedException"></exception>
/// <exception cref="ObjectDisposedException"></exception>
/// <exception cref="SocketException"></exception>
public static bool Online(this Socket socket)
{
if (socket == null) return false;
var pending = socket.Poll(1000, System.Net.Sockets.SelectMode.SelectRead);
var available = socket.Available;
var offline = pending && available == 0;
return !offline;
}
/// <summary>从套接字接收数据。</summary>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentOutOfRangeException"></exception>
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="SocketException"></exception>
/// <exception cref="ObjectDisposedException"></exception>
/// <exception cref="System.Security.SecurityException"></exception>
public static byte[] Receive(this Socket socket, int maxLength = 1024)
{
if (socket == null) throw new ArgumentNullException(nameof(socket));
if (maxLength < 0 || maxLength > 65535) throw new ArgumentOutOfRangeException(nameof(maxLength));
var buffer = new byte[maxLength];
var received = socket.Receive(buffer, 0, maxLength, SocketFlags.None);

41
Apewer/Network/TcpClient.cs

@ -1,13 +1,6 @@
using Apewer;
using Apewer.Internals;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace Apewer.Network
{
@ -16,13 +9,19 @@ namespace Apewer.Network
public class TcpClient : IDisposable
{
Socket _client = null;
Socket _socket = null;
/// <summary>套接字实例。</summary>
public Socket Socket { get => _client; }
public Socket Socket { get => _socket; }
/// <summary>已连接。</summary>
public bool Connected { get => _client != null && _client.Connected; }
/// <summary>在线。</summary>
public bool Online { get => Extensions.Online(_socket); }
/// <summary>本地终结点。</summary>
public IPEndPoint LocalEndPoint { get; private set; }
/// <summary>远程终结点。</summary>
public IPEndPoint RemoteEndPoint { get; private set; }
/// <summary>启动客户端,并连接到服务端。</summary>
public TcpClient(string ip, int port) : this(IPAddress.Parse(ip), port) { }
@ -33,18 +32,20 @@ namespace Apewer.Network
/// <summary>启动客户端,并连接到服务端。</summary>
public TcpClient(IPEndPoint endpoint)
{
_client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_client.Connect(endpoint);
_client.SendTimeout = 5000;
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_socket.Connect(endpoint);
_socket.SendTimeout = 5000;
LocalEndPoint = _socket.LocalEndPoint as IPEndPoint;
RemoteEndPoint = endpoint;
}
/// <summary>关闭连接,释放系统资源。</summary>
public void Dispose()
{
try { _client.Disconnect(false); } catch { }
try { _client.Close(100); } catch { }
try { _socket.Disconnect(false); } catch { }
try { _socket.Close(100); } catch { }
#if !NET20
try { _client.Dispose(); } catch { }
try { _socket.Dispose(); } catch { }
#endif
}
@ -54,13 +55,13 @@ namespace Apewer.Network
/// <exception cref="SocketException"></exception>
/// <exception cref="ObjectDisposedException"></exception>
/// <exception cref="System.Security.SecurityException"></exception>
public byte[] Receive(int maxLength = 1024) => _client.Receive(maxLength);
public byte[] Receive(int maxLength = 1024) => _socket.Receive(maxLength);
/// <summary>发送。</summary>
/// <exception cref="ArgumentNullException" />
/// <exception cref="SocketException" />
/// <exception cref="ObjectDisposedException" />
public void Send(byte[] data) => _client.Send(data);
public void Send(byte[] data) => _socket.Send(data);
}

Loading…
Cancel
Save