最近在unity中实现接受屏幕分享的功能。昨天程序还运行得好好的,不知道今天怎么不停捕捉到错误,但捕捉到的错误又没有提示,所以请教一下各位大佬们。
private void ReceiveCallback(IAsyncResult result)
{
int read = client.EndReceive(result);
//Debug.Log(read);
try
{
if (read > 0)
{
if (data == null)
{
dataLength = BitConverter.ToInt32(buffer, 4);
currentLength = 0;
data = new byte[dataLength];
Array.ConstrainedCopy(buffer, 4, data, currentLength, read - 4);
currentLength = read - 4;
}
else
{
int rest = dataLength - currentLength;
if (rest < read)
read = rest;
Array.ConstrainedCopy(buffer, 0, data, currentLength, read);
currentLength += read;
if (currentLength >= dataLength&& !dataHandle.IsFull())
{
dataHandle.SaveByteArray(data);
data = null;
}
}
}
else
{
client.Shutdown(SocketShutdown.Both);
client.Close();
}
}
catch (Exception e)
{
Debug.Log(e.Message);
client.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveCallback, null);
}
client.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveCallback, null);
}