Click to See Complete Forum and Search --> : Network programming


ejlynch
01-29-2004, 09:12 AM
I have created a sender and receiver program that sends packets to each other using UDP. I want to be able to specify the size of the packets that they send from the commandline. This is programmed in C. Taking the value from the commandline is no problem.

I have created a structure that contains details about the packet etc... but I want to include a data part in this structure. What is contained in the data portion doesnt matter just as long as it makes the paket a certain size.

What is the best way to create the data part so that i can send the entire struture over a socket ?

If i create a dynamic array of chars (string) using malloc and have a pointer in the structure. will the actual memory that the pointer points to be sent inside the struture using the socket?

jim mcnamara
01-29-2004, 12:11 PM
You do know that structs get padded. For example integers are usually word-aligned, with leading fill.

You will need to test what you create: printf("%d\n", sizeof(struct mypacket));

A buffer of unsigned char or void * might work better.

tecknophreak
01-29-2004, 05:05 PM
Just a few quick questions:

1. Are you going to change the struct size on each side?
2. Are you going to set the size on startup or throughout the program?

I'm pretty sure that when you send udp packets and you change size, the receiver should know what size the next packet will be. I.E. I was running a program which sent UDP packets with a header containing the size and some other information. With TCP you can send a packet and the receiver can read the first x bytes then go back and read the next x bytes. However, I read my header and lost the rest of the bytes with UDP. Just a heads up.

GaryJones32
01-30-2004, 02:26 AM
the kernel already does this stuff on it's own
this is from the man page
UDP fragments a packet when its total length exceeds the interface mtu. A better more polite alternative is
to use path MTU discovery and send data in right sized chunks so that no fragmentation occurs. Linux 2.2
automatically keeps track off the path MTU of a specific destination when the IP_PMTU_DISCOVER
socket option is set to IP_PMTUDISC_DO. UDP will return an EMSGSIZE error then when the data-
gram length exceeds the path MTU and save the new pmtu in the error queue if enabled (this is a path mtu
recovery event). To bootstrap the pmtu discovery on unconnected sockets it is possible to start with a big
datagram size of 65536 and let it shrink by pmtu discovery events. When the socket is connected to a spe-
cific peer with connect(2) the path mtu can be retrieved conveniently using the IP_MTU socket option.t
For connectionless sockets with many destinations the discovered MTU per destination can be accessed
using the error queue after a pmtudisc recovery event occurred (see ip(4) for an description of the error
queue). The application shall lower its packet sizes then to the new path MTU. To get an initial estimate of
the PMTU it is possible to connect an temporary socket to the destination address and retrieve the MTU
using the IP_MTU getsockopt

ejlynch
01-30-2004, 09:05 AM
1. Are you going to change the struct size on each side?


Yes. This will be passed as a commandline argument to both the sender and the receiver.


2. Are you going to set the size on startup or throughout the program?

The size will be set on startup. There will be no change in the packet size throughout the program.


Effectively, I am creating a UDP probing application, something like ping.