4

I know how to send byte data to a server via the udp protocol. How can I activate some flags on the package that I am sending? How can I tel if a package that is received has some flags activated. Moreover how can I read the checksum contained by the package? If I want to send a package with no data and just a flag how can I do that? I want to do this with c#. I dont want to modify the local endpoint nor the remote endpoint nor anything else about each package.

Edit

The reason why I want to do this is because I have tried so hard to do tcp punch holing. I opened a bounty question at forward traffic from port X to computer B with c# "UDP punch hole into firewall" anyways I managed to do udp punch holing. As a result I want to make udp reliable.

In other words I want to create an algorith that will enable me to send udp packages reaching their destination in the right order. If I could set on and read some of the flags then that will facilitate this algorithm.

I know it will not be eassy to create but at least I want to give it a try. I been waisting a lot of time finding a c# library that does this...

Community
  • 1
  • 1
Tono Nam
  • 34,064
  • 78
  • 298
  • 470
  • 1
    I'm affraid you either have to use raw sockets which are limited on windows XP now or create your own protocol containing a sequence number and checksum together with the actual data – Polity Sep 07 '11 at 06:41

2 Answers2

1

In all honesty, I think it's going to be just a difficult to get the TCP side of things working as it is to tackle this question, and since TCP is designed to do what your asking, then I personally would take the "Right tool, for the right job" approach.

That said....

If you really must use UDP, then you can forget about adding anything at protocol level, the underlying transport layer will just ignore it.

Instead your going to have to design your own byte level protocol (Almost going back to the days of RS232 and serial comms...)

Your going to need your data, and that's going to have to be wrapped in some way, say a byte that defines start of packet, then maybe the data, or some flags etc...

I would look at data structures such as TLV (Used by a lot of smart cards) - TLV stands for Tag-Length-Value, so might represent something like:

    0x10 0x04 0x01 0x02 0x03 0x04

Where command code 0x10 might mean update ticker, 0x04 - 4 bytes of data to follow... then 4 bytes of data.

As for flags, and check sums, well flags are easy, you can get 8 of them in a byte, and just flip them using AND masks..

    0x00 AND 0x01 = 0x01
    0x01 AND 0x80 = 0x81

Check sums are a little harder, but it really depends what level of accuracy you need. Essentially a check sum is some kind of mathematical algorithm that uses the contents of your data to create some magical number that can be easily computed at the receiving end.

This could be something as simple as adding all the values together then anding the result by some magic number, then adding that onto the end of your packet before sending the bytes.

Then do the same at the receiving end and compare the results.

Of course if you really don't want to go down this route of wrapping, doing protocols and all that jazz yourself, then you could learn a lesson from the bad old days of serial comms.

There are still plenty of open source implementations of things like XModem, ZModem, Kermit, YModem and many others still floating around, since most of these can (and did) work with byte streams, then it shouldn't be too difficult to make them work with a byte stream over UDP instead of a byte stream over a serial port.

shawty
  • 5,729
  • 2
  • 37
  • 71
  • Thanks a lot. That sounds intresting. Yeah I guess you are rirght... I will have to create my flags and check sum with the bytes that I send. I did not knew about a lot of the things you mentioned but I guess you made a good job explaining that. So I believe that now I will have to send that ckeck sum at the byte level. I plan to send a positive int32 as the checksum and lets say that 8 posible flags therefore that means thay I will have to send if I do the math right about 5 bytes of data for evey thing I plan to send right? Then the other party will treat the first 5 bytes as the header info – Tono Nam Sep 07 '11 at 08:28
  • Yup, that makes a lot of sense. You just need to remember to document it thoroughly, however you decide, so the receiving party knows how to deal with it. – shawty Sep 07 '11 at 11:03
  • @ shawty and let's say that I need to send 1024 bytes of data. I will place the first first 5 bytes (my header) in front of that array giving me an array of length 1029 right. When I send that byte array of length 1029 will that split into two packages? I mean will I have to spit the initial 1024 array into two arrays each of 512 bytes and placing a header into each? I mean how often should I place the header (add the first 5 bytes)? – Tono Nam Sep 07 '11 at 15:57
  • Yes it will split your data packets on the packet size boundary, but as an application level programmer you shouldn't need to worry about that. Even though UDP lacks the control that TCP has, the packets are still split and re-assembled by the network stack at both ends of the connection. If your looking to exert control at the transport level then NONE of what's been discussed here will do you any good, you will need to use TCP pure and simple. If you need an insight, look up the "ISO 7 Layer protocol diagram" – shawty Sep 07 '11 at 20:57
1

UDP already has a checksum.

Did you mean to ask about something like RUDP? This allows you to guarantee packet delivery and ordering (both, none, or either). Lidgren implements a reliable UDP protocol.

If you want to roll your own, read up on how SEQ and ACK work in TCP; and emulate that over UDP (which is what Lidgren basically does).

Jonathan Dickinson
  • 9,050
  • 1
  • 37
  • 60
  • @ Jonathan true there are several libraries out there that do what I am trying to do. I have also found some but there are very few on c# . I have not been able to found one on c# that works. Or maybe I am doing something wrong. I will rather use a library that already works. Because I have not been able to make one of them work I am about to start my own. If you find one and show me a basic example I will greatly apreciate it. – Tono Nam Sep 07 '11 at 14:13
  • @Tono Lidgren G3 has NAT punching built right in. I did also give you information on the bit in TCP that makes it ordered/reliable (ACK and SEQ). – Jonathan Dickinson Sep 07 '11 at 14:17
  • There are so many libraries that do the same thing and I got tired of it. Thats why today I was planing to built my own. I will give a try to yours though . Hope I can make it work! – Tono Nam Sep 07 '11 at 14:23
  • @Tono it isn't 'my' library; but I have used it and I know it works. – Jonathan Dickinson Sep 07 '11 at 14:36