Problemas de C# con el tamaño de bytes de envío/recepción (sockets)

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

Este es el enviar código:

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");

Y aquí está el receive code:

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;                    

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.

The error is at this statement read += stream.Read(data, read, data.Length - read);

preguntado el 31 de julio de 12 a las 14:07

You should definately tackle the situation where the sender closes the connection while the receiver still expects additional data (stream.Read() volvería 0 en ese caso). -

1 Respuestas

I would try getting smaller chunks of data. In your code you're starting off with todos 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);

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.

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.

Respondido 31 Jul 12, 14:07

i know but why does it receive 2GB , its supposed to only receive 5kb - Tarek Adel

Are you sure 5k is being sent? what are the values in the numberofbytesArray? Also, are you sure nothing else reads anything from the stream before calling ReceiveImage? The user of BitConverter is right, so the only way you wouldn't get the right values is if the Image.Length isn't 5K, or something already read all or some of those bytes which corrupts how you read the lenght... - pedro ritchie

YEA IT WAS 5KB , BUT I DIDNT READ numberofbytesArray , how can i read the value in it?? - Tarek Adel

There's something more going on that you haven't provided, the code you posted should work; I'm trying to get the root of the problem--guessing at possibilities because you haven't provide code that reproduces the problem. - pedro ritchie

do you think it might be the imagetobyte() function?? - Tarek Adel

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.