0

我不断收到此错误:

"The IAsyncResult object was not returned from the corresponding asynchonous method on this class. Parameter name : aysncResult. Line 105.

当我尝试连接到本地服务器时会发生这种情况;它出错并且无法连接。

这是我的客户代码:

public class Client
{

    public delegate void OnConnectEventHandler(Client sender, bool connected);
    public event OnConnectEventHandler OnConnect;

    public delegate void OnSendEventHandler(Client sender, int sent);
    public event OnSendEventHandler OnSend;

    public delegate void OnDisconnectEventHandler(Client sender);
    public event OnDisconnectEventHandler OnDisconnect;

    Socket socket;

    public bool Connected
    {
        get
        {
            if (socket != null)
            {
                return socket.Connected;
            }

            return false;
        }
    }

    public Client()
    {
        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    }

    public void SendData()
    {

    }

    public void Connect(string IP, int port)
    {
        if (socket == null)
        {
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        }

        socket.BeginConnect(IP, port, new AsyncCallback(sendCallback), null);


    }

    private void connectCallback(IAsyncResult ar)
    {
        //try
        //{
            socket.EndConnect(ar);

            if (OnConnect != null)
            {
                OnConnect(this, Connected);
            }
        //}
        //catch
        //{

        //}
    }

    public void Send(byte[] data, int index, int length)
    {
        socket.BeginSend(BitConverter.GetBytes(length), 0, 4, SocketFlags.None, new AsyncCallback(sendCallback), null);
        socket.BeginSend(data, index, length, SocketFlags.None, new AsyncCallback(sendCallback), null);
    }

    private void sendCallback(IAsyncResult ar)
    {
        try
        {
            int sent = socket.EndSend(ar); ( errrors here )

            if (OnSend != null)
            {
                OnSend(this, sent);
            }
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.ToString());
            return;
        }
    }

    public void Disconnect()
    {
        try
        {
            if (socket.Connected)
            {
                socket.Close();
                socket = null;
                if (OnDisconnect != null)
                {
                    OnDisconnect(this);
                }
            }
        }
        catch
        {

        }
    }
4

1 回答 1

1

你不应该有两个挂起的BeginSend操作。

完成后发送大小和缓冲区:

public void Send(byte[] data, int index, int length)
{
    //add data as state
    socket.BeginSend(BitConverter.GetBytes(length), 0, 4, SocketFlags.None, sendCallback, data);
}

private void sendCallback(IAsyncResult ar)
{
    try
    {
        int sent = socket.EndSend(ar); ( errrors here )

        // check if data was attached.
        if (ar.AsyncState != null)
        {
            byte[] buffer = (byte[])ar.AsyncState;
            socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, sendCallback, null);
            return;
        }

        if (OnSend != null)
        {
            OnSend(this, sent);
        }
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.ToString());
        return;
    }
}

您还可以使用BeginSend采用缓冲区列表的重载。

于 2013-01-10T06:21:00.930 回答