0

So I was trying to send a file yesterday over my TCP Socket Client to my Server and I realized pretty quick that you cannot send more than 65k bytes of data at a time.

This quickly became an issue since I was trying to send an image that was 260k bytes.

So my thought process was this.

First I would send a packet containing the size of the image so that the server then knows how much to read and then in the next go I would start sending all the bytes from the image.

Since I'm dealing with asynchronous sockets it's not really waiting or blocking after I send the first packet.

So I have no idea how to first send the size so I know how much to read and then actually start reading the data because I would have to send it in chunks from the server at 1024 bytes for each chunk

This is what I am doing from the Client

var BUFFER = new byte[1024];
                    //Set the BUFFER Type
                    BUFFER[0] = 2;

//This just returns the image path from a image on the desktop.
                    var ImageBuffer = ImageHelper.GetImage();
                    var IMAGE_SIZE_BUFFER = BitConverter.GetBytes(ImageBuffer.Length);

                    Buffer.BlockCopy(IMAGE_SIZE_BUFFER, 0, BUFFER, 1, IMAGE_SIZE_BUFFER.Length);


                    CLIENT_SOCKET.Send(BUFFER, 0, BUFFER.Length,
                        SocketFlags.None);

and on the server I am doing this

        var BUFFER_SIZE = CLIENT_SOCKET.EndReceive(ar);

        //Create a packet that has the length of the BUFFER_SIZE
        var PACKET = new byte[BUFFER_SIZE];

        //Copy over the content from the RECEIVED_BUFFER[] to the PACKET[] from 0 to PACKET.Length.
        Array.Copy(RECEIVED_BUFFER, PACKET, PACKET.Length);

        //Determine the packet Type by reading the first byte of the array.
        var PACKET_TYPE = RECEIVED_BUFFER[0];

        //Interpret the actual data skipping the first byte since that's the type.
        var ACTUAL_PACKET = RECEIVED_BUFFER.Skip(1);

        //Check what type of packet we received.
        switch (PACKET_TYPE)
        {

            case 1:
                break;

            //Image Send
            case 2:
                //stores the int value of the packet size
                var packetSize = BitConverter.ToInt32(ACTUAL_PACKET.ToArray(),0);
                //Receive the first packet & block


                //Start receiving the second packet. (Keep reading until it hit the size of the variable)
                //This is where I get stuck
                break;

        }

        //Begin receiving data again
        CLIENT_SOCKET.BeginReceive(RECEIVED_BUFFER, 0, RECEIVED_BUFFER.Length, SocketFlags.None,
            ReceivedDataCallback, CLIENT_SOCKET);
Mark Denom
  • 987
  • 1
  • 8
  • 24
  • What's the sense of these all-uppercase variable names? That's really unreadable. Besides that, consider using a standard protocol for file transfer, like FTP or HTTP PUT or POST. – Clemens Jun 04 '19 at 12:45
  • Good point, I just got stuck doing those as I wrote that example, now heres the issue, creating the header of the packet containing the length and the type is fine, it's on the server end, how do I keep receiving to the same byte array – Mark Denom Jun 04 '19 at 12:51
  • That's going to become complicated very quickly. If at all possible, don't do this yourself. Use a protocol stack that implements it for you. See https://learn.microsoft.com/en-us/windows/desktop/Http/http-api-start-page – Clemens Jun 04 '19 at 13:11

0 Answers0