1
我目前正在将一个硬件库移植到.NET Core上。通信在TCP上工作。我有'Socket.BeginReceive'方法的问题。 MSDN如何在.NET Core中从Socket接收数据?
看来在.NET Core中没有等价的方法。我怎样才能从TCP套接字接收数据?
private void InternalDataReceived(IAsyncResult ar)
{
int dataCount = 0;
byte[] buffer;
try
{
if (_client != null && _client.Client != null)
{
dataCount = _client.Client.EndReceive(ar);
}
if (dataCount > 0)
{
try
{
buffer = new byte[dataCount];
Array.Copy(_inBuffer, buffer, dataCount);
if (DataReceived != null)
{
DataReceived(buffer);
}
}
catch (Exception exc)
{
if (exc is System.Net.Sockets.SocketException)
{
Disconnect();
return;
}
}
_client.Client.BeginReceive(_inBuffer, 0, _inBuffer.Length, SocketFlags.None, InternalDataReceived, null);
}
}
catch
{
Disconnect();
}
}
有[同步](https://docs.microsoft.com/en-us/dotnet/core/api/system.net.sockets。 Socket#System_Net_Sockets_Socket_Receive_System_Byte ___)和[异步](https://docs.microsoft.com/en-us/dotnet/core/api/system.net.sockets.socket#System_Net_Sockets_Socket_ReceiveAsync_System_Net_Sockets_SocketAsyncEventArgs_)版本的.NET Core中的接收。 –