Sending packages (UDP), how to split them?

Okay so i know how to send packages. But i also now the limit (64kb), which means.
Anything more will be impossible to send in one package.

So my problem is, how do i send stuff that´s bigger?
I am guessing i must split with a for loop or something, but i don´t know how to do it.

                byte[] u = imageToByteArray(holder);
                byte[] buff = new byte[512];
                int c = 0;
                for (int i = 0; i < u.Length; i++)
                    buff[c] = u[i];
                    udpcap.Send(buff, buff.Length, adress.Address.ToString(), 1700);


Here is an example, but well it doesn´t work, i get (Index was outside the bounds of the array.)

So any ideas?
The question you should really be asking is how to receive them. Sending fragmented data over UDP means it can arrive in any order, and may miss some (or all) fragments. Are you sure you don't need TCP?
I would like to try it in UDP first.

Is UDP a problem on LAN btw, or is it only in "real Internet" application that it can be a mess?

And isn´t it possible to receive it in udp?
I don´t really care if the result is partly corrupted, i would just like to get it working first.

Wouldn't it make more sense to do it the other way round; get things up and running with TCP and then switch to UDP if that isn't suitable.

Why do you want UDP in the first place?


PS If you're having trouble figuring out how to split something up with a "for loop or something", then UDP is prob something to avoid if possible...
Last edited on
maybe, does it look the same?
Is it simple to switch?

As i don´t think i will ever drop a package in my situation, and probably not get them out of sync either(though it may happen).

But i can do whatever, i just want to be able to do it as a start.

Why i want UDP is, low latency, it´s a must in my case.

I will test TCP if you think it´s better for this (as many do).
but i need an example, as i just don´t get how to write it.

The only thing i want to do is

Take a image
change it to byte array (as i think you must do this to send it?)
send the image
receive the image
decode it, and show it

The problem is only, sending and receiving.
i can do it with 1 package (with udp as i know how it works)
but i am unable to do it with more udp packages, and totally unable to do it with tcp no matter how many packages as it works differently.

I am in desperate need of help:(
An image is already an array of bytes. You just need to send and receive the data.

send and recv work pretty much the same with TCP and UDP. The problem with sending multiple packets with UDP is that you're not supposed to assume the rder, so you would usually mark each packet with a sequence number and then use that information to check you've got all the required packets and to help you re-assemble the packets once you've received them.

In the TCP case, the packets will arrive in order. So you just have receive in a loop until everthing's arrived. But you do have to astablish the connection first.

See the following sample for more info:

Running the Winsock Client and Server Code Sample

I have read but i don´t understand at all how to send in TCP.
From what i grasp, you are able to send an image(file etc) without dividing, as TCP does that for you.

But that´s it, i have no idea how to send.

As you don´t write the same as UDP where it´s straightforward, you send this array, it´s that long, to that IP.

Tcp only asks for an array/buffer, that´s it.

Topic archived. No new replies allowed.