开发者

Get and set udp packages flags and checksum with c#

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 开发者_StackOverflowfacilitate 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...


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).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜