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中的接收。 –