“描述符不是套接字”异常

时间:2018-12-14 09:26:36

标签: c# sockets unity3d

手动断开套接字后出现异常:

  

描述符不是套接字UnityEngine.Debug:Log(Object)   ClientTCP:OnReceive()(位于Assets / Scripts / ClientTCP.cs:95)   ClientTCP:ConnectCallback(IAsyncResult)(在   资产/脚本/ClientTCP.cs:37)System.Net.Sockets.Worker:Connect()

我认为当启用“ closed”标志时,我的“ while(!closed)OnReceive()”周期应该停止。

当该方法获得0字节时,它应显示“与服务器断开连接”并将该标志设置为True。

实际上就是这样:我在Unity中收到“与服务器断开连接”消息。

我没有更改代码中的任何内容,但现在更改为“ _clientSocket.Disconnect(true);”我遇到了提到的异常。

您能看看我在做什么错吗?

using System;
using System.Net.Sockets;
using System.Net;
using Bindings;
using UnityEngine;

public class ClientTCP : MonoBehaviour
{
    public static Socket _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    private byte[] _asyncbuffer = new byte[1024];
    private bool closed;

    public string IP_ADDRESS;
    public int PORT;

    public void ConnectToServer()
    {
        closed = false;
        Debug.Log("Connecting to server...");
        _clientSocket.BeginConnect(IP_ADDRESS, PORT, new AsyncCallback(ConnectCallback), _clientSocket);
    }

    private void ConnectCallback(IAsyncResult asyncResult)
    {
        _clientSocket.EndConnect(asyncResult);
        while (!closed)
        {
            OnReceive(); //Line 37
        }

    }

    public void DisconnectFromServer()
    {
        _clientSocket.Disconnect(true);

        closed = true;
    }

    private void OnReceive()
    {
        byte[] _sizeinfo = new byte[4];
        byte[] _receivedbuffer = new byte[1024];

        int totalread = 0, currentread = 0;

        try
        {
            currentread = totalread = _clientSocket.Receive(_sizeinfo); //Line 58

            if (totalread <= 0)
            {
                Debug.Log("Disconnected from server");
                closed = true;
            }
            else
            {
                while (totalread < _sizeinfo.Length && currentread > 0)
                {
                    currentread = _clientSocket.Receive(_sizeinfo, totalread, _sizeinfo.Length - totalread, SocketFlags.None);
                    totalread += currentread;
                }

                int messagesize = 0;
                messagesize |= _sizeinfo[0];
                messagesize |= (_sizeinfo[1] << 8);
                messagesize |= (_sizeinfo[2] << 16);
                messagesize |= (_sizeinfo[3] << 24);

                byte[] data = new byte[messagesize];

                totalread = 0;
                currentread = totalread = _clientSocket.Receive(data, totalread, data.Length - totalread, SocketFlags.None);

                while (totalread < messagesize && currentread > 0)
                {
                    currentread = _clientSocket.Receive(data, totalread, data.Length - totalread, SocketFlags.None);
                    totalread += currentread;
                }

                ClientHandleNetworkData.HandleNetworkInformation(data);
            }
        }
        catch (Exception ex)
        {
            Debug.Log(ex.Message); //Line 95, here the exception is thrown
            closed = true;
        }
    }

ex.ToString()提供以下内容:

  

System.Net.Sockets.SocketException:描述符不是套接字
  在System.Net.Sockets.Socket.Receive(System.Byte []缓冲区)[0x00000]   在ClientTCP.OnReceive()的:0中:[0x0001e]在   /Users/nidere/Projects/Unity/OverWaste/Assets/Scripts/ClientTCP.cs:58   UnityEngine.Debug:Log(Object)ClientTCP:OnReceive()(在   资产/脚本/ClientTCP.cs:95)   ClientTCP:ConnectCallback(IAsyncResult)(在   资产/脚本/ClientTCP.cs:37)System.Net.Sockets.Worker:Connect()

58行是:

currentread = totalread = _clientSocket.Receive(_sizeinfo);

0 个答案:

没有答案