不接收客户端上的所有数据

发布于 2024-11-06 08:08:01 字数 3537 浏览 0 评论 0原文

可能的重复:
C# Begin/EndReceive - 如何读取大数据?
C# 异步套接字服务器接收问题

我在套接字上编写了异步客户端和服务器应用程序(TCP)。例如,当我从服务器发送 300000 字节时,我在客户端仅收到其中的一部分。然后我再次调用 Receive 方法并接收其余数据。虽然Receive方法必须接收所有数据。问题是什么?

Here the code of the Receive method on client side:

 public class ConnectionInfo
 {
        public Socket _socketReply;
        public Socket _SocketCommand;
        public byte[] _bufferSendReply;
        public byte[] _bufferReceive;
        public byte[] _bufferSendCommand;
        public int _numberBytesSentReply = 0;
        public int _numberBytesSentCommand = 0;
        public List<byte> _fullBufferReceive;
        public uint _id = 0;

        public ConnectionInfo()
        {
            _fullBufferReceive = new List<byte>();
        }
 }

 private void RecieveCallback(IAsyncResult asyncResult)
    {
        ConnectionInfo connection = (ConnectionInfo)asyncResult.AsyncState;
        try
        {
            int bytesRead = connection.Socket.EndReceive(asyncResult);

            if (bytesRead > 0)
            {
                for (int i = 0; i < bytesRead; i++)
                    connection.FullBufferReceive.Add(connection.BufferReceive[i]);
                if (bytesRead == connection.BufferReceive.Length)
                {
                    connection.Socket.BeginReceive(connection.BufferReceive, 0, connection.BufferReceive.Length, 0,
                                                   new AsyncCallback(RecieveCallback), connection);
                    Console.WriteLine("Bytes recieved -- " + bytesRead + " by " + connection.Id);
                }
                else
                {
                    Console.WriteLine("All -- Bytes -- recieved " + bytesRead + " by " + connection.Id);

                    ConnectionInfo NewCI = new ConnectionInfo(connection);
                    Recieve(connection);
                    _serverController.SpreadReceivedData(NewCI);
                }
            }
            else
                CloseConnection(connection);
        }
        catch (ObjectDisposedException ode)
        {
            CloseConnection(connection);
            Console.WriteLine("ObjectDisposedException: " + ode.Message);
        }
        catch (SocketException exc)
        {
            CloseConnection(connection);
            Console.WriteLine("Socket exception: " + exc.SocketErrorCode);
        }
        catch (Exception e)
        {
            CloseConnection(connection);
            Console.WriteLine(e.ToString());
        }
    }

 public void Recieve(ConnectionInfo connection)
 {
        try
        {
            connection.BufferReceive = new byte[Settings.bufferLength];
            connection.FullBufferReceive.Clear();
            connection.NumberBytesSentReply = 0;

            connection.Socket.BeginReceive(connection.BufferReceive, 0, connection.BufferReceive.Length, 0,
                                          new AsyncCallback(RecieveCallback), connection);
        }
        catch (SocketException exc)
        {
            CloseConnection(connection);
            Console.WriteLine("Socket exception: " + exc.SocketErrorCode);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

Possible Duplicates:
C# Begin/EndReceive - how do I read large data?
C# Async Sockets Server Receive Problems

I have asynchronous client and server applications written on Sockets(TCP). When I send, for example, 300000 bytes from the server, I receive only a part of them on the client. And then I call the Receive method again and receive the rest of the data. Although the Receive method has to receive all the data. What is the problem?

Here the code of the Receive method on client side:

 public class ConnectionInfo
 {
        public Socket _socketReply;
        public Socket _SocketCommand;
        public byte[] _bufferSendReply;
        public byte[] _bufferReceive;
        public byte[] _bufferSendCommand;
        public int _numberBytesSentReply = 0;
        public int _numberBytesSentCommand = 0;
        public List<byte> _fullBufferReceive;
        public uint _id = 0;

        public ConnectionInfo()
        {
            _fullBufferReceive = new List<byte>();
        }
 }

 private void RecieveCallback(IAsyncResult asyncResult)
    {
        ConnectionInfo connection = (ConnectionInfo)asyncResult.AsyncState;
        try
        {
            int bytesRead = connection.Socket.EndReceive(asyncResult);

            if (bytesRead > 0)
            {
                for (int i = 0; i < bytesRead; i++)
                    connection.FullBufferReceive.Add(connection.BufferReceive[i]);
                if (bytesRead == connection.BufferReceive.Length)
                {
                    connection.Socket.BeginReceive(connection.BufferReceive, 0, connection.BufferReceive.Length, 0,
                                                   new AsyncCallback(RecieveCallback), connection);
                    Console.WriteLine("Bytes recieved -- " + bytesRead + " by " + connection.Id);
                }
                else
                {
                    Console.WriteLine("All -- Bytes -- recieved " + bytesRead + " by " + connection.Id);

                    ConnectionInfo NewCI = new ConnectionInfo(connection);
                    Recieve(connection);
                    _serverController.SpreadReceivedData(NewCI);
                }
            }
            else
                CloseConnection(connection);
        }
        catch (ObjectDisposedException ode)
        {
            CloseConnection(connection);
            Console.WriteLine("ObjectDisposedException: " + ode.Message);
        }
        catch (SocketException exc)
        {
            CloseConnection(connection);
            Console.WriteLine("Socket exception: " + exc.SocketErrorCode);
        }
        catch (Exception e)
        {
            CloseConnection(connection);
            Console.WriteLine(e.ToString());
        }
    }

 public void Recieve(ConnectionInfo connection)
 {
        try
        {
            connection.BufferReceive = new byte[Settings.bufferLength];
            connection.FullBufferReceive.Clear();
            connection.NumberBytesSentReply = 0;

            connection.Socket.BeginReceive(connection.BufferReceive, 0, connection.BufferReceive.Length, 0,
                                          new AsyncCallback(RecieveCallback), connection);
        }
        catch (SocketException exc)
        {
            CloseConnection(connection);
            Console.WriteLine("Socket exception: " + exc.SocketErrorCode);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文