当前位置:编程学习 > C#/ASP.NET >>

socket receive循环读取send来的数据

//send 图片流
  public void SendBinary(byte[] b)
        {
            try
            {
                socket.Send(b, b.Length, 0);
            }
            catch (Exception)//如果有异常则退出
            {
                socket.Close();
            }
        }
  //receive 图片流
  public byte[] ReceiveBinary()
        {

            //Byte[] buffer = new Byte[socket.Available];//一个缓存区
            ////循环从缓冲区读取数据
            //do
            //{
            //    socket.Receive(buffer);
            //}
            //while (socket.Available > 0);
            //return buffer;

            //循环从缓冲区读取数据
            string receiveData = "";
            int recvCount;//每次读取的数据量
            int availCount = 0;//获取从网络可以读取的数据量
            Byte[] buffer = new Byte[8192];//一个8×1024bits的缓存区
            do
            {
                availCount = socket.Available;

                if (availCount < buffer.Length)
                {
                    recvCount = socket.Receive(buffer, availCount, SocketFlags.None);
                }
                else
                {
                    recvCount = socket.Receive(buffer, buffer.Length, SocketFlags.None);
                }

                availCount = socket.Available;
                if (recvCount > 0)
                {
                    receiveData += Encoding.UTF8.GetString(buffer, 0, recvCount);
                }
            }
            while (availCount > 0);
            byte[] b = System.Text.Encoding.UTF8.GetBytes(receiveData);
            return b;

        } --------------------编程问答--------------------
引用楼主 dreamyxcy 的帖子:
//send   图片流 
    public   void   SendBinary(byte[]   b) 
                { 
                        try 
                        { 
                                socket.Send(b,   b.Length,   0); 
                        } 
                        catch   (Exception)//如果有异常则退出 
                        { 
                                socket.Close(); 
                    …


很好
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,