i have the following code :
int * buffer;
buffer = (int *) malloc((320*240)*sizeof(int));
after populating the buffer with data i want to send it using UDP sockets, however it seems that currently my following send function sends just 4 byte (of data) and not ((320*240)*sizeof(int)):
iError = send(MySock, (char *)(buffer) , sizeof(buffer), 0);
i am tried different combinations on the second and third fields of the send function but it doesn’t work, What is the correct syntax should i use?
BTW i have to define the buffer as int * since it being used by other library that has to get that as int *.
This is because in your
send()call, you specifysizeof (buffer). This is the size of the variable buffer, which is the size of a pointer, which happens to be four bytes on your system. It’s NOT the size of the block that the pointer points at, that information is not available.You must specify
320 * 240 * sizeof *bufferto get the proper size. Of course, even better is to factor this out into a variable:There’s no need to cast,
malloc()returnsvoid *andsend()accepts it.Note that doing very large
send()s over UDP might not work, due to IP fragmentation. It’s better to send it using many small datagrams, in my experience.