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

保留中の操作を 2 つ持つべきではありません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 に答える