NetSupportReadBytesFromSocket 方法 (Socket, Int32, ActionInt64, Int64, Boolean, Boolean) |
读取socket数据的基础方法,只适合用来接收指令头,或是同步数据
命名空间:
HslCommunication.Core
程序集:
HslCommunication (在 HslCommunication.dll 中) 版本:8.2.1.0 (8.2.1.0)
语法 public static byte[] ReadBytesFromSocket(
Socket socket,
int receive,
Action<long, long> report,
bool reportByPercent,
bool response
)
Public Shared Function ReadBytesFromSocket (
socket As Socket,
receive As Integer,
report As Action(Of Long, Long),
reportByPercent As Boolean,
response As Boolean
) As Byte()
public:
static array<unsigned char>^ ReadBytesFromSocket(
Socket^ socket,
int receive,
Action<long long, long long>^ report,
bool reportByPercent,
bool response
)
static member ReadBytesFromSocket :
socket : Socket *
receive : int *
report : Action<int64, int64> *
reportByPercent : bool *
response : bool -> byte[]
参数
- socket
- 类型:System.Net.SocketsSocket
通信对象 - receive
- 类型:SystemInt32
接收的长度 - report
- 类型:SystemActionInt64, Int64
用于报告接收进度的对象 - reportByPercent
- 类型:SystemBoolean
是否按照百分比报告进度 - response
- 类型:SystemBoolean
是否回发接收数据长度
返回值
类型:
Byte接收到的字节数据
异常 异常 | 条件 |
---|
ArgumentNullException | |
SocketException | |
ObjectDisposedException | |
SecurityException | |
示例
接收数据的举例,输出报告,不根据百分比来产生报告,不回复接收进度。
Socket socket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
socket.Connect( IPAddress.Parse( "192.168.0.7" ), 1000 );
Action<long, long> report = ( long rece, long totle ) =>
{
Console.WriteLine( "总数据量:" + totle + " 当前接收字节数:" + rece );
};
byte[] receive = NetSupport.ReadBytesFromSocket( socket, 10000, report, false, false );
socket.Close( );
参见