字节(套接字)的发送/接收大小的C#问题 [英] C# Problems With Sending/Receiving Size of bytes (Sockets)

查看:68
本文介绍了字节(套接字)的发送/接收大小的C#问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了这些代码来使用TCP套接字发送和接收图像,但是接收代码无效.

I've made these codes to send and receive an Image with a TCP socket but the receive code didn't work.

这是发送代码:

public void SendImage()
{
    int ScreenWidth = Screen.GetBounds(new Point(0, 0)).Width;
    int ScreenHeight = Screen.GetBounds(new Point(0, 0)).Height;
    Bitmap bmpScreenShot = new Bitmap(ScreenWidth, ScreenHeight);

    Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
    gfx.CopyFromScreen(0, 0, 0, 0, new Size(ScreenWidth, ScreenHeight));
    bmpScreenShot.Save(Application.StartupPath + "/ScreenShot.jpg", ImageFormat.Jpeg);
    byte[] image = new byte[1];
    bmpScreenShot = ResizeBitmap(bmpScreenShot, 300, 300);

    image = ImageToByte(bmpScreenShot);
    //get the length of image (length of bytes)
    int NumberOfBytes = image.Length;
    //put the size into a byte array
    byte[] numberofbytesArray = BitConverter.GetBytes(NumberOfBytes);

    //send the size to the Client
    int sizesend = sck.Send(numberofbytesArray, 0, numberofbytesArray.Length, 0);
    if (sizesend > 0)
    {
        MessageBox.Show("Size Sent");
    }
    //send the image to the Client
    int imagesend =sck.Send(image, 0, NumberOfBytes, 0);
    if (imagesend > 0)
    {
        MessageBox.Show("Image Sent");
    }
}

这是接收代码:

public void ReceiveImage()
{
    if (sck.Connected)
    {
        {
            NetworkStream stream = new NetworkStream(sck);
            byte[] data = new byte[4];

            //Read The Size
            stream.Read(data, 0, data.Length);
            int size = (BitConverter.ToInt32(data,0));
            // prepare buffer
            data = new byte[size];

            //Load Image
            int read = 0;
            while (read != data.Length)
            {
               read += stream.Read(data, read, data.Length - read);
            }
            //stream.Read(data, 0, data.Length);
            //Convert Image Data To Image
            MemoryStream imagestream = new MemoryStream(data);
            Bitmap bmp = new Bitmap(imagestream);
            pictureBox1.Image = bmp;                    
        }
    }
}

问题是当我发送大小时,其发送为5kb,但是当我收到它时,我发现它为2GB,出现此错误:

The problem is when i send the size, its sent as 5kb but when i receive it i find it 2GB and this error comes up:

无法从传输连接中读取数据.由于系统缺少足够的缓冲区空间或队列已满,因此可以对套接字执行操作.

Unable to read data from the transport connection. An operation on a socket could be performed because the system lacked sufficient buffer space or because a queue was full.

此语句中的错误 read + = stream.Read(data,read,data.Length-read);

推荐答案

我会尝试获取较小的数据块.在您的代码中,您首先要使用 all 数据(一次2GB的数据将出现故障.将其降低到较小的值–数据将以分块的形式发送.例如:

I would try getting smaller chunks of data. In your code you're starting off with all the data (2GB at a time in the case that fails. Drop that down to something smaller--the data is sent in chunks anyway. For example:

read += stream.Read(buffer, read, 20480);

每次读取大约2k,以便不大于缓冲区空间或对于队列而言太大.

this will read about 2k at a time so as to not be larger than the buffer space or be too large for the queue.

如果您分配了2GB的缓冲区,则您的应用程序可能只剩很少的内存了.基础框架可能无法为其自身分配2GB的数据(已分配的总容量为4GB)来传输数据.

If you've allocated a buffer of 2GB in size, your application likely has very little memory left. The underlying framework is probably unable to allocated 2GB of data for itself (4GB total allocated) to transfer data.

这篇关于字节(套接字)的发送/接收大小的C#问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆