异步TCP套接字不能在c#中工作

本文关键字:工作 不能 TCP 套接字 异步 | 更新日期: 2023-09-27 17:51:07

我一直得到这个错误:

"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
        {
        }
    }

异步TCP套接字不能在c#中工作

您不应该有两个未完成的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重载,它接受一个缓冲区列表